0

I execute rar command with some file, that i cant attach here

But this problem appear only on that file, in many other - everything ok

Command looks like:

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

The case:

    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();

Return 0 code, ok

But if i just replace getInputStream and waitFor, it will execute forever

Code:

    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;

In method process.waitFor() it will be forever cycle

I set an Thread.sleep(100000), but it do not help

    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;

The process takes little time, just 1-2 seconds in command line

So:

  1. Is it a normal situation, that i must do something with process to get exit code before? I guess, that process must be executed in other thread, and i dont need to yank any additional methods? Why does it happend? Why Process command waitFor() can be forever executed without getInputStream() calling?

  2. Is there a 100% solution, for call exit code, that will not be executed forever?

  3. Can rar utility block somehow getting exit code method?

  • Why would you want to wait for the process to finish *before* you have read its output? – VGR Feb 15 '21 at 15:24
  • I dont need process output, i need only exit code, cause output is dependend on localization and sometimes it is not right to parse output text, the best choose - is to get exit code – Bestows In Constructing Feb 15 '21 at 15:40
  • Issue with stdIO stream buffer deadlock? Try this https://stackoverflow.com/a/18955510/185565. You need to start eating an IO buffer before waitFor() function. – Whome Feb 15 '21 at 15:40
  • If you don’t need the output, call [inheritIO()](https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/lang/ProcessBuilder.html#inheritIO()) on your ProcessBuilder, then don’t bother reading the output at all. – VGR Feb 15 '21 at 15:46

0 Answers0