11

I normally use java.lang.ProcessBuilder and java.lang.Process to run external command line programs, and it works fine for run-and-done commands. For example, this would run "myProgram" with 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, say I wanted to run a script or program or something that had interactive input (it prompted me for more input after starting). Can I do that in Java with code similar to that above, or do I need a different approach? Or is there some library that can help me with this?

Brendan Long
54.6k21 gold badges154 silver badges194 bronze badges
asked Sep 11, 2012 at 19:53

2 Answers 2

5

According to the documentation you should be able to redirect the input and output streams. This tells it to use the System.in/System.out from the parent process:

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

If you want to write things to the processes's input:

If the source is Redirect.PIPE (the initial value), then the standard input of a subprocess can be written to using the output stream returned by Process.getOutputStream(). If the source is set to any other value, then Process.getOutputStream() will return a null output stream.

answered Sep 11, 2012 at 19:59
Sign up to request clarification or add additional context in comments.

3 Comments

with stdin and stdout you mean System.in and System.out?
I used this (including redirecting the error stream) to launch /usr/bin/vi from a Java command-line program and it behaved exactly as I had hoped.
this is absolutely WRONG answer because this NOT opens interactive pseudo terminal this it is useless because child will buffer thus prevent ANY interaction.
0

Redirecting stdin and stdout is certainly one option for simple command-line programs.

Using a "robot" class is another, if you actually need to script keystrokes (for example, in a test script):

Writing a simple .bat file or shell script, that calls your Java program and uses "<" and ">" redirection operators is yet a third option.

It all depends on exactly what you're looking for :)

answered Sep 11, 2012 at 20:06

4 Comments

I basically want to force through confirmation prompts for a program that doesn't have a --force flag or equivalent.
Can you run the program in a shell script? If "yes", would that be sufficient?
@smcg - As I said, "It all depends on exactly what you're looking for :)". I just wanted to make sure you had all available answers. Good luck! Sincerely .. PSM
with stdin and stdout you mean System.in and System.out?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.