2

I have some simple code that uses Java apache exec classes to run external processes.

  Executor ex = new DefaultExecutor();
  ex.setStreamHandler(new PumpStreamHandler(System.out, System.out, System.in));
  CommandLine cl = new CommandLine(
     "C:\\program.exe");

     ex.execute(cl);

}

For certain command line programs, this works as expected and gets all the program's output into the "out" stream while accepting my own text into the "in" stream. However, for other programs, the output of the process is visible running manually from command line, but I don't get anything coming in when I run via java process.

I would like to eventually write to the stdin and retrieve and analyze stdout within the code itself.

If there a reason that I don't know of, why some programs seem to output text on the command line, yet when I run them as java processes, I don't receive anything through the streams?

This is happening in Windows.

  • maybe you have the issue from this question: http://stackoverflow.com/questions/27249101/windows-prevents-stdout-from-external-command-line-executions-to-be-redirected-t – radio Dec 02 '14 at 12:09

1 Answers1

0

Out of process code will not go to the same command line output unless you explicitly configure it to do so. Also, as a general rule it is better to use a logging library like log4j than to do println statements.

smcg
  • 3,195
  • 2
  • 30
  • 40
  • I would like to eventually be able to write to the process stdin and read from its stdout within my java code itself, mimicking a human at the prompt. Is that possible with this limitation? – user1309154 Apr 03 '12 at 13:40
  • You can, but it's annoying. Use getOutputStream/getInputStream in the Process class to configure. But I have read that if you do that within the external process itself it can produce deadlocks. There are workarounds (apparently you can do the configuration in another process). Alternately it seems like you can use ProcessBuilder to redirect i/o streams: http://stackoverflow.com/questions/3643939/java-process-with-input-output-stream – smcg Apr 03 '12 at 13:58
  • I may be misunderstanding. But in my code I am passing the System.out and in Streams into the PumpStreamHandler constructor, an Apache exec class which I belive does this of streams internally. My question is that for some external programs, including dummy programs I write myself, I can successfully retrieve all output, yet some other external programs give me blank output even though at the windows cmd line, they output properly. – user1309154 Apr 03 '12 at 14:07