Run interactive command line application from java

I usually use java.lang.ProcessBuilder and java.lang.Process to run external command line programs, and it works great for run-and-done commands. For example, this would launch "myProgram" with the argument "myArg" in the working directory:

List<String> commandLine = new ArrayList<String>();
commandLine.add("myProgram");
commandLine.add("myArg");
ProcessBuilder builder = new ProcessBuilder(commandLine);
builder.redirectErrorStream(true);
Process process = builder.start();

However, let's say I wanted to run a script or program or something that had an interactive input (this caused me more input after starting). Can I do this in Java with code similar to the above, or do I need a different approach? Or is there a library that can help me?

+5
source share
2 answers

, . System.in/System.out :

builder.redirectInput(ProcessBuilder.Redirect.INHERIT);
builder.redirectOutput(ProcessBuilder.Redirect.INHERIT);

:

Redirect.PIPE( ), , Process.getOutputStream(). - , Process.getOutputStream() .

+4

All Articles