Java ProcessBuilder waitfor()从不竞争

时间:2018-03-29 07:26:48

标签: java

下面是我的代码。想要执行python脚本但是waitfor()永远不会完成。以下是我的代码。任何建议。

String[] command ={"CMD","C:\\Users\\vkode200\\IdeaProjects\\Pythonex1\\TestHello.py"}; 

         ProcessBuilder probuilder = new ProcessBuilder(command );
            //You can set up your work directory
            /*probuilder.directory(new File("c:\\xyzwsdemo"));*/

            Process process = probuilder.start();

            //Read out dir output
            InputStream is = process.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line;
            System.out.printf("Output of running %s is:\n",
                    Arrays.toString(command));
            /*while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
*/
            //Wait to get exit value
            try {
                 exitValue = process.waitFor();
                 /*exitValue= process.exitValue();*/
                System.out.println("\n\nExit Value is " + exitValue);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

1 个答案:

答案 0 :(得分:0)

在调用stdout之前,您需要关闭流程的输入流,并在stderrwaitFor()上使用其所有输出。

否则可能会阻止尝试读取您不发送的输入,或产生您无法阅读的输出。