Skip to content

Instantly share code, notes, and snippets.

@ifedorenko
Created July 9, 2015 12:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ifedorenko/39948b6693745ab87e90 to your computer and use it in GitHub Desktop.
Save ifedorenko/39948b6693745ab87e90 to your computer and use it in GitHub Desktop.
commons-exec 1.3

== TestMain.java

public class TestMain {
  public static void main(String[] args) {
    for (String arg : args) {
      System.out.println(arg);
    }
  }
}

== CommonsExecMain.java

import java.io.File;
import java.io.IOException;

import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ShutdownHookProcessDestroyer;

public class CommonsExecMain {
  public static void main(String[] args) throws IOException {
    String executable =
        System.getProperty("java.home") + File.separator + "bin" + File.separator + "java";
    if (File.separatorChar == '\\') {
      executable = executable + ".exe";
    }
    CommandLine cli = new CommandLine(executable);
    cli.addArguments(new String[] {"-cp", new File("target/classes").getCanonicalPath()});
    cli.addArgument(TestMain.class.getName());
    cli.addArgument("some text with spaces");

    DefaultExecutor executor = new DefaultExecutor();
    executor.setProcessDestroyer(new ShutdownHookProcessDestroyer());
    executor.setWorkingDirectory(new File("target").getCanonicalFile());
    executor.execute(cli);
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment