Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run command line from Java code in the background?

I have the following line to run a batch file,

Process process = Runtime.getRuntime().exec("cmd /c start rake.bat");

But I want it to run in the background and not display the command line to the user. How can I change it to do this?

The problem is that the command window opens and intrupts the programs GUI. I just want the command window to not be visible while it is executing the batch file.

like image 788
cHam Avatar asked Jul 09 '12 11:07

cHam


People also ask

How do I run a Java process in the background?

The & symbol, switches the program to run in the background. The nohup utility makes the command passed as an argument run in the background even after you log out.

How do you run a command in the background explain in detail?

If you want to run additional commands while a previous command runs, you can run a command in the background. If you know you want to run a command in the background, type an ampersand (&) after the command as shown in the following example. The number that follows is the process id.


1 Answers

Removing the 'start' completely will do what you want (as this is what is creating the window):

Process process = Runtime.getRuntime().exec("cmd /c rake.bat");

I have tested this and it works, ofcourse if you want to communicate with the command prompt you'd have to have Input and Output streams, also not forgetting your Error stream As stated in the comment though removing 'start' on XP wont help (as it wont work).

like image 162
David Kroukamp Avatar answered Oct 01 '22 06:10

David Kroukamp