为什么进程命令 waitFor() 可以在没有 getInputStream() 调用的情况下永远执行?

问题描述

我用一些文件执行 rar 命令,我不能在这里附加

但是这个问题只出现在那个文件上,在许多其他文件中 - 一切正常

命令看起来像:

rar x /tmp/junit15696402387580789438/ОС.rar /tmp/junit15696402387580789438

案例:

    Process process = new ProcessBuilder(commandarray).start();
    try (Reader reader = new InputStreamReader(process.getInputStream())) {
        CharStreams.toString(reader);
    } catch (Exception e) {
        getLogger().error(e);
    }
    return process.waitFor();

返回0代码,好的

但是如果我只是替换getInputStream和waitFor,它将永远执行

代码

    Process process = new ProcessBuilder(commandarray).start();
    int code = process.waitFor();
    try (Reader reader = new InputStreamReader(process.getInputStream())) {
        CharStreams.toString(reader);
    } catch (Exception e) {
        getLogger().error(e);
    }
    return code;

方法 process.waitFor() 中,它将永远循环

我设置了 Thread.sleep(100000),但没有帮助

    Process process = new ProcessBuilder(commandarray).start();
    try {
        Thread.sleep(100000);
    } catch (InterruptedException e) {
        e.printstacktrace();
    }
    int code = process.waitFor();
    try (Reader reader = new InputStreamReader(process.getInputStream())) {
        CharStreams.toString(reader);
    } catch (Exception e) {
        getLogger().error(e);
    }
    return code;

该过程花费的时间很少,在命令行中只需 1-2 秒

所以:

  1. 这是正常情况吗,我必须先对进程做一些事情才能获得退出代码?我想,该进程必须在其他线程中执行,我不需要拉任何其他方法? 为什么会发生?为什么 Process 命令 waitFor() 可以在不调用 getInputStream() 的情况下永远执行?

  2. 是否有 100% 的解决方案,对于调用退出代码,不会永远执行?

  3. rar 实用程序能否以某种方式阻止获取退出代码方法

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)