【发布时间】:2018-04-29 14:02:32
【问题描述】:
我正在尝试运行从 java 中的 cpp 程序创建的可执行文件。如果我双击 exe 文件,它工作得很好,但是如果我使用 ProcessBuilder 运行该文件,它会由于某种原因卡住,它会打印大部分预期的输出并且不会继续,也使整个 Java 程序无法运行回应。 这是我的代码:
String filePath = FirstScreenController.getFile().getPath();
ProcessBuilder launcher = new ProcessBuilder("ClusteringProgram\\Release\\main.exe",filePath);
launcher.redirectErrorStream(true);
try {
/*File file = FirstScreenController.getFile();
Path newPath = Paths.get(System.getProperty("user.dir")+"\\ClusteringProgram").resolve("K12.fasta");//Moving the file to the
Files.copy(Paths.get(file.getPath()), newPath, StandardCopyOption.REPLACE_EXISTING);*/
System.out.println("Execution started");
p = launcher.start();
InputStream stderr = p.getInputStream();
InputStreamReader isr = new InputStreamReader(stderr);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
p.waitFor();//Waiting for the process to finish running
System.out.println("Execution completed");
} catch (IOException | InterruptedException e) {e.printStackTrace();}
【问题讨论】:
-
程序没有结束,它没有打印整个输出。同样,如果我在 Java 之外双击可执行文件,它工作正常。 @Someprogrammerdude
-
寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参阅:如何创建 minimal reproducible example。使用edit 链接改进您的问题 - 不要通过 cmets 添加更多信息。谢谢!
-
@GhostCat:这里有足够的信息。它只是看起来不像。对于任何需要提出问题的人来说,MVCE 都是不可能的。
-
这是一个有效的问题,不是题外话,下面已经回答了。
标签: java pipe deadlock processbuilder