gpt4 book ai didi

java - 尝试在java中运行从cpp文件创建的exe文件

转载 作者:行者123 更新时间:2023-12-01 06:22:05 24 4
gpt4 key购买 nike

我正在尝试运行由 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();}

最佳答案

关闭您的信息流。这就是导致你挂起的原因。我经常写这样的代码。

    while ((line = br.readLine()) != null) {
System.out.println(line);

}
br.close(); // You need this or p can hang
p.waitFor();

此外,您调用了 launcher.redirectStandardError(true); 所以您实际上需要所有这些来将 stdout 和 stderr 收集在一起: 这个答案的其余部分是错误的。我不知道是什么导致了僵局。我将大的代码片段留在这里,以防出现一些奇怪的库错误,并且事实证明需要双线程读取技术来解决它。

    final object lock = new object();
InputStream stdout = p.getInputStream();
InputStreamReader isr = new InputStreamReader(stdout);
BufferedReader br = new BufferedReader(isr);
final InputStream stderr = p.getErrorStream();
one = new Thread() {
public void run() {
InputStreamReader isr2 = new InputStreamReader(stderr);
BufferedReader br2 = new BufferedReader(isr2);
while ((line2 = br2.readLine()) != null) {
synchronized(lock) {
System.out.println(line2);
}
}
br2.close(); // you need this or p can hang
}
};
one.start();
while ((line = br.readLine()) != null) {
synchronized(lock) {
System.out.println(line);
}
}
br.close(); // You need this or p can hang
for (;;) {
try {
one.join();
break;
} catch (InterruptedException v) {
/* if there's something that might want the main thread's attention handle it here */
}
}
p.waitFor();

关于java - 尝试在java中运行从cpp文件创建的exe文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50087419/

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