Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Runtime.getRuntime().exec() platform independent?

Is

Runtime.getRuntime().exec("cmd /c");

platform independent or I have to pass platform specific command in the parameter?

Thanks


What is the linux equivalent of the command cmd /c start /b java -jar

like image 700
Tapas Bose Avatar asked Mar 06 '26 01:03

Tapas Bose


1 Answers

Runtime.getRuntime().exec() is platform independant in the way, that it will start an external executable on every platform. The executable itself has to be there of course, so starting notepad.exe on Linux will very likely not work (except when you have Wine and Notepad installed, but this is another story).

On Windows, enter anything you would enter into cmd.exe (details follow). But to do the automatic path lookup on Windows, you need to do something like

Runtime.getRuntime().exec("start iexplore.exe");

or

Runtime.getRuntime().exec("start my.pdf");

which opens the pdf file with the associated viewer.

On linux, you can do anything you would do in a shell like Bash, but you cannot use bash builtins like the pipe operator. You can just start programs and pass arguments.

To start another Java instance on linux you could use:

Runtime.getRuntime().exec(new String[] {"java","-jar","myjar.jar"});

Use the absolute path to the java executable if it is not on the PATH.

like image 66
Daniel Avatar answered Mar 08 '26 15:03

Daniel



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!