使用Java程序启动本地QQ进程

public class Test {
        public static void main(String[] args) throws Exception {
            //Process p=r.exec("E:\\Program File\\KGMusic\\KuGou.exe") 为什么它就不能被销毁
            List<String> cmd = Arrays.asList("D:\\majinpeng\\QQ\\Bin\\QQ.exe");
            ProcessBuilder pb = new ProcessBuilder(cmd);
            Process p = pb.start();
            new Thread(new WaitForTimeout(p,1));
            BufferedInputStream bis = new BufferedInputStream(p.getInputStream());
            byte[] bytes = new byte[1024];
            int len = 0;
            StringBuilder sb = new StringBuilder(1024);
            while ((len = bis.read(bytes)) != -1){
                sb.append(new String(bytes,0,len));
            }
            System.out.println(sb.toString());
            bis.close();
        }
    static class WaitForTimeout implements Runnable {

        private Process process;
        private int timeout;

        WaitForTimeout(Process process, int timeout) {
            this.process = process;
            this.timeout = timeout;
        }

        @Override
        public void run() {
            try {
                if (!process.waitFor(timeout, TimeUnit.MILLISECONDS)) {
                    if (process != null) {
                        process.destroy();
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

你可能感兴趣的:(使用Java程序启动本地QQ进程)