== 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);
}
}