Jared
Jared

Reputation: 159

How do I fork an external process in java

I'm trying to fork a new external process (such as Calculator) in Java. I'm new to operating systems but I learned that it's possible using something like : Runtime.getRuntime ().exec ("C:\\Windows\\system32\\calc.exe");. However that doesn't actually fork a new process. Is there anyway I can fork an external process using java?

Upvotes: 3

Views: 3320

Answers (1)

Elliott Frisch
Elliott Frisch

Reputation: 201447

I suggest you prefer a ProcessBuilder over Runtime.exec. Also, if I understand your qestion, then you can pass the full path to the exe file to your ProcessBuilder. Something like,

ProcessBuilder pb = new ProcessBuilder("C:\\Windows\\system32\\calc.exe");
pb.inheritIO(); // <-- passes IO from forked process.
try {
    Process p = pb.start(); // <-- forkAndExec on Unix
    p.waitFor(); // <-- waits for the forked process to complete.
} catch (Exception e) {
    e.printStackTrace();
}

Upvotes: 4

Related Questions