gpt4 book ai didi

Java ProcessBuilder waitfor()从不竞争

转载 作者:太空宇宙 更新时间:2023-11-04 10:35:20 25 4
gpt4 key购买 nike

下面是我的代码。尝试执行 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();
}

最佳答案

在调用 waitFor() 之前,您需要关闭进程的输入流,并在 stdoutstderr 上消耗其所有输出。

否则,尝试读取您未发送的输入或生成您未读取的输出时可能会被阻止。

关于Java ProcessBuilder waitfor()从不竞争,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49550297/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com