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?

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.

4
  • with stdin and stdout you mean System.in and System.out?
    – 11684
    Sep 11, 2012 at 20:13
  • Thank you! Been looking for this for quite awhile.
    – dantiston
    Feb 25, 2016 at 1:09
  • 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. Jun 10, 2018 at 14:37
  • this is absolutely WRONG answer because this NOT opens interactive pseudo terminal this it is useless because child will buffer thus prevent ANY interaction.
    – Enerccio
    Apr 24 at 11:24
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 :)

4
  • I basically want to force through confirmation prompts for a program that doesn't have a --force flag or equivalent.
    – smcg
    Sep 11, 2012 at 20:11
  • Can you run the program in a shell script? If "yes", would that be sufficient?
    – paulsm4
    Sep 11, 2012 at 20:33
  • @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
    – paulsm4
    Sep 11, 2012 at 22:25
  • with stdin and stdout you mean System.in and System.out?
    – 11684
    Sep 12, 2012 at 14:28

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.