1

I found the following code which uses Runtime.getRuntime().exec to run an arbitrary program (like Notepad.exe ) .

public class RuntimeDemo {

   public static void main(String[] args) {
   try {

   // create a new array of 2 strings
   String[] cmdArray = new String[2];

   // first argument is the program we want to open
   cmdArray[0] = "notepad.exe";

   // second argument is a txt file we want to open with notepad
   cmdArray[1] = "example.txt";

   // print a message
   System.out.println("Executing notepad.exe and opening example.txt");

   // create a process and execute cmdArray and currect environment
   Process process = Runtime.getRuntime().exec(cmdArray,null);

   // print another message
   System.out.println("example.txt should now open.");

   } catch (Exception ex) {
   ex.printStackTrace();
   }

   }
}

I ran this from Eclipse, and it opens Notepad.exe (or I can use calc.exe also ) .

But what if we want to run an arbitrary Java command from this program, something like :

java -version -

will this run? I see a problem here is that .. where do you see the results of it? If I'm running command-line, it comes out to the command line.. but here?

Caffeinated
  • 11,982
  • 40
  • 122
  • 216
  • 3
    *"will this run?"* - The question is, have you tried? I would encourage you to use `ProcessBuilder` of `Runtime.exec` it's easy to configure things like the working directory and redirecting the error stream through the process output stream. As you've noted, the example is not consuming the `Process`'s `InputStream`, which allows you to read the output from the `Process`, which would be the next thing you would need to solve – MadProgrammer Jul 10 '15 at 01:10
  • 2
    For [example](http://stackoverflow.com/questions/19968759/how-to-read-the-std-output-of-another-java-program-in-this-java-program/19969016#19969016) and [example](http://stackoverflow.com/questions/19968759/how-to-read-the-std-output-of-another-java-program-in-this-java-program/19969016#19969016) – MadProgrammer Jul 10 '15 at 01:10
  • @MadProgrammer - Thanks so much !! I'm on it – Caffeinated Jul 10 '15 at 01:15
  • @MadProgrammer - I tried my hand at it, still not flying though - http://stackoverflow.com/questions/31331717/having-trouble-running-my-program-via-the-getruntime-in-java – Caffeinated Jul 10 '15 at 02:59
  • @MadProgrammer - Why do we need ProcessBuilder , and not just Process – Caffeinated Jul 10 '15 at 03:00
  • 1
    `ProcessBuilder` allows you to change the working directory of that the `Process` is executed in, it allows you to redirect the stdout/err to different locations (like a file or merge them into a single stream for easier processing). I also believe it's executes the command differently from `Runtime#exec`, which has helped solve a number of other problems – MadProgrammer Jul 10 '15 at 03:07

1 Answers1

2
  1. Use ProcessBuilder of Runtime#exec, it provides a more configurable solution and encourages the use of String[] or List<String> for the commands and parameters, which solves a bunch of issues, especially when your parameters contain spaces.
  2. Read the Process's InputStream

For example...

try {
    String[] command = {"java.exe", "-?"};
    ProcessBuilder pb = new ProcessBuilder(command);
    pb.redirectErrorStream(true);
    Process exec = pb.start();

    BufferedReader br = new BufferedReader(new InputStreamReader(exec.getInputStream()));
    String text = null;
    while ((text = br.readLine()) != null) {
        System.out.println(text);
    }

    System.out.println("Process exited with " + exec.waitFor());
} catch (IOException | InterruptedException exp) {
    exp.printStackTrace();
}

Which outputs

Usage: java [-options] class [args...]
           (to execute a class)
   or  java [-options] -jar jarfile [args...]
           (to execute a jar file)
where options include:
    -d32      use a 32-bit data model if available
    -d64      use a 64-bit data model if available
    -server   to select the "server" VM
                  The default VM is server.

    -cp <class search path of directories and zip/jar files>
    -classpath <class search path of directories and zip/jar files>
                  A ; separated list of directories, JAR archives,
                  and ZIP archives to search for class files.
    -D<name>=<value>
                  set a system property
    -verbose:[class|gc|jni]
                  enable verbose output
    -version      print product version and exit
    -version:<value>
                  require the specified version to run
    -showversion  print product version and continue
    -jre-restrict-search | -no-jre-restrict-search
                  include/exclude user private JREs in the version search
    -? -help      print this help message
    -X            print help on non-standard options
    -ea[:<packagename>...|:<classname>]
    -enableassertions[:<packagename>...|:<classname>]
                  enable assertions with specified granularity
    -da[:<packagename>...|:<classname>]
    -disableassertions[:<packagename>...|:<classname>]
                  disable assertions with specified granularity
    -esa | -enablesystemassertions
                  enable system assertions
    -dsa | -disablesystemassertions
                  disable system assertions
    -agentlib:<libname>[=<options>]
                  load native agent library <libname>, e.g. -agentlib:hprof
                  see also, -agentlib:jdwp=help and -agentlib:hprof=help
    -agentpath:<pathname>[=<options>]
                  load native agent library by full pathname
    -javaagent:<jarpath>[=<options>]
                  load Java programming language agent, see java.lang.instrument
    -splash:<imagepath>
                  show splash screen with specified image
See http://www.oracle.com/technetwork/java/javase/documentation/index.html for more details.
Process exited with 0

This assumes that java.exe is within the PATH environment, otherwise you may be required to provide the full path to it (or change the working directory)

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • For some reason, Eclipse didn't like this part - `catch (IOException | InterruptedException exp)` – Caffeinated Jul 10 '15 at 03:32
  • What is the use of the pipe here? if i just say `public static void main(String[] args) throws InterruptedException` will that suffice ? – Caffeinated Jul 10 '15 at 03:33
  • 1
    It's simply a "multi" catch block, makes handling multiple exceptions easier by allowing you to group them together. Prior to this, you would need to have a single `catch` for each exception, which was pain if you wanted to handle them in the same way – MadProgrammer Jul 10 '15 at 03:38
  • so this is like a Java 1.8 feature I'm guessing . sounds really neat ! – Caffeinated Jul 10 '15 at 03:48
  • 1
    Can't remember if it's 7 or 8, but it's very neat ;) – MadProgrammer Jul 10 '15 at 04:06
  • 1
    It’s Java 7 syntax. And when you are using Java 7 or newer, you may simply call [`inheritIO()`](http://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html#inheritIO()) on the `ProcessBuilder` to make the subprocess print its output directly to your own console/output, making the entire `readLine`&`println` loop unnecessary. It even allows to enter text on the console if the subprocess requires input… – Holger Jul 10 '15 at 13:30
  • 1
    @Holger I've had issues with `inheritIO` before, sometimes it makes it easier to read output from python, but other times, it makes it do nothing... – MadProgrammer Jul 10 '15 at 13:32