Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 398
Jared Avatar asked Nov 18 '25 21:11

Jared


1 Answers

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();
}
like image 114
Elliott Frisch Avatar answered Nov 21 '25 11:11

Elliott Frisch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!