0

I was successfully using AutoIt to execute commands but I was thinking I could get a more stable implementation via Runtime. That way I know the commands will always be executed and won't get thrown by Interruption exceptions, and other random crap. Is there something about Runtime that I don't know which won't allow for continuous execution of commands? Does it not have a memory for the outputs of previous commands, i.e. is it not running in a persistent command line?

The following commands navigate to a folder and execute a Maven script. How would I get this to work? If there were 10+ more commands that follow, would they execute within in the same process?

sendCommand("cmd.exe cd homepath/plugins");
sendCommand("mvn archetype:generate -DarchetypeCatalog=file://homepath/.m2/repository");

private static void sendCommand(String text) throws IOException {
    Runtime.getRuntime().exec(text);
}

2 Answers 2

2

Runtime.exec() returns a Process instance. Call waitFor() on this object to wait until it is complete before running a next command. You can communicate with a Process via its getInputStream()/getOutputStream() methods.

Also read the Javadoc. For Runtime.exec it says "Executes the specified string command in a separate process."

1
  • Okay so if my script has say 20 command lines then I'll want them all in one process exec? Jun 3, 2016 at 14:55
1

A few things.

  1. You should use Process and ProcessBuilder instead.
  2. The commands have to be split up and tokenized according to arguments.
  3. The way you have it written, those two commands will not be executed in the same process.
  4. Fortunately for you, ProcessBuilder supports changing the working directory of the command anyway.

As an example:

sendCommand("homepath/plugins", "mvn", "archetype:generate", "-DarchetypeCatalog=file://homepath/.m2/repository");

private static void sendCommand(String workingDirectory, String... command) throws IOException {
    Process proc = new ProcessBuilder(command).directory(new File(workingDirectory)).start();
    int status = proc.waitFor();
    if (status != 0) {
        // Handle non-zero exit code, which means the command failed
    }
}

Notice how a) the command has been split up, and b) that the working directory is passed in and set using ProcessBuilder.directory(File). This will get your desired behavior, but note that each command will still be a separate process, and there's no way to combine them with Java. You'd have to use Maven's features to get them all to run at once by specifying multiple build targets.

2
  • What about this? stackoverflow.com/a/18867097 It looks like I can add && between commands in order to run all of them within the same process? Jun 6, 2016 at 9:20
  • @bluemunch That will combine them all into a single command, but it won't combine them into a single process ID. Now, it will combine them all into a single java.util.Process. If that's what you mean by a single process, then the && will work perfect for you.
    – Brian
    Jun 6, 2016 at 14:31

Your Answer

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

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