I am a newbie using Java to do some data processing on csv files. For that I use the multithreading capabilities of Java (pools of threads) to batch-import the csv files into Ja
There are several ways to start a new process in Java:
ProcessBuilder
Runtime.exec()
With ProcessBuilder
:
ProcessBuilder pb =
new ProcessBuilder("myCommand", "myArg1", "myArg2");
Map env = pb.environment();
env.put("VAR1", "myValue");
env.remove("OTHERVAR");
env.put("VAR2", env.get("VAR1") + "suffix");
pb.directory(new File("myDir"));
File log = new File("log");
pb.redirectErrorStream(true);
pb.redirectOutput(Redirect.appendTo(log));
Process p = pb.start();
assert pb.redirectInput() == Redirect.PIPE;
assert pb.redirectOutput().file() == log;
assert p.getInputStream().read() == -1;
With Runtime
:
Runtime r = Runtime.getRuntime();
Process p = r.exec("firefox");
p.waitFor(10, TimeUnit.SECONDS);
p.destroy();
With Apache Commons Exec:
String line = "AcroRd32.exe /p /h " + file.getAbsolutePath();
CommandLine cmdLine = CommandLine.parse(line);
DefaultExecutor executor = new DefaultExecutor();
int exitValue = executor.execute(cmdLine);
Key differences between Multiprocessing and Multithreading from this:
Additional links: