Java: Redirecting inner process output when running from command line

Alexey

I use the following code, for redirecting the output of a process I launch from my Java app:

ProcessBuilder builder = new ProcessBuilder("MyProcess.exe");
builder.redirectOutput(Redirect.INHERIT);
builder.redirectErrorStream(true);

Now, this works fine when I run the code from eclipse - I can see the output in Eclipse's console.
Yet when I create a jar file and run it from a cmd window, e.g. java -jar MyJar.jar, it doesn't print the output of the process. What could be the reason for this?

Kevin Workman

I know I'm late in answering, but I came across this question before coming across the answer, and wanted to save anybody else in the same boat some searching.

This is actually a known bug for Windows: https://bugs.openjdk.java.net/browse/JDK-8023130

You can get around it by redirecting the streams yourself:

Process p = pb.start();
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = br.readLine()) != null) {
   System.out.println(line);
}

p.waitFor();

br.close();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Running java in package from command line

From Dev

Prevent figures from closing when running a script from the command line

From Dev

Execute a command in Java without redirecting the output

From Dev

Error when running JUnit Tests from DOS Command line

From Dev

JVM variables escape equals when running from command line

From Dev

Maven Java application crashes when running from command line but not from within the Netbeans IDE

From Dev

Java Command Line Output

From Dev

Download file as csv when running script from command line

From Dev

Python redirecting output from an LSF command

From Dev

Output is incomplete when running jdb as a process from java application

From Dev

Ignoring single PHPCS rule when running from command line

From Dev

Redirecting the output of a process into the input of another process using ProcessBuilder in Java

From Dev

Java: No such file or directory when redirecting command line program output to /dev/null

From Dev

Getting output from running command line app in Inno Setup

From Dev

How to swap out to a new file a running process output is redirecting to, without restarting the command?

From Dev

safely shut down a running process by process name from command line

From Dev

Running a Java Eclipse Project from Command line

From Dev

Command line sorting file content and redirecting output to the file

From Dev

No output when running powershell command

From Dev

How to display console output when running program from command prompt

From Dev

Python redirecting output from an LSF command

From Dev

cronjob not redirecting output of command when used with option

From Dev

Redirecting output of running process via SSH in background

From Dev

Output is incomplete when running jdb as a process from java application

From Dev

command not found error on Mac when running mfp from command line

From Dev

Running this from command line in Java

From Dev

What are the columns in the output of running a jmeter test from the command line?

From Dev

Errors When Running Java from Command Prompt

From Dev

Scanner not breaking when reading a line from a Process' output stream

Related Related

  1. 1

    Running java in package from command line

  2. 2

    Prevent figures from closing when running a script from the command line

  3. 3

    Execute a command in Java without redirecting the output

  4. 4

    Error when running JUnit Tests from DOS Command line

  5. 5

    JVM variables escape equals when running from command line

  6. 6

    Maven Java application crashes when running from command line but not from within the Netbeans IDE

  7. 7

    Java Command Line Output

  8. 8

    Download file as csv when running script from command line

  9. 9

    Python redirecting output from an LSF command

  10. 10

    Output is incomplete when running jdb as a process from java application

  11. 11

    Ignoring single PHPCS rule when running from command line

  12. 12

    Redirecting the output of a process into the input of another process using ProcessBuilder in Java

  13. 13

    Java: No such file or directory when redirecting command line program output to /dev/null

  14. 14

    Getting output from running command line app in Inno Setup

  15. 15

    How to swap out to a new file a running process output is redirecting to, without restarting the command?

  16. 16

    safely shut down a running process by process name from command line

  17. 17

    Running a Java Eclipse Project from Command line

  18. 18

    Command line sorting file content and redirecting output to the file

  19. 19

    No output when running powershell command

  20. 20

    How to display console output when running program from command prompt

  21. 21

    Python redirecting output from an LSF command

  22. 22

    cronjob not redirecting output of command when used with option

  23. 23

    Redirecting output of running process via SSH in background

  24. 24

    Output is incomplete when running jdb as a process from java application

  25. 25

    command not found error on Mac when running mfp from command line

  26. 26

    Running this from command line in Java

  27. 27

    What are the columns in the output of running a jmeter test from the command line?

  28. 28

    Errors When Running Java from Command Prompt

  29. 29

    Scanner not breaking when reading a line from a Process' output stream

HotTag

Archive