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.

1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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?
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: stackoverflow.com/questions/3643939/…
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.