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

标签 java pipe deadlock processbuilder

我正在尝试运行由 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/

相关文章:

java - Tapestry - 接收两个上下文并在表单中使用它们

c - 管道上的持久 execvp?

python - 如何捕获 stdout/stderr 中的提示?

multithreading - 如何避免更新死锁

java - hibernate 异常 LockAcquisitionException : could not update

java - 如何使用 arg 'T value' 模拟方法?

java - java中的打印方法

sql-server - SQL Server表值函数锁问题

java - 如何在组件的某些特定区域捕获鼠标移动事件?

performance - DB2 concat与双管道