By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,241 Members | 760 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,241 IT Pros & Developers. It's quick & easy.

Problem with Runtime.getRuntime().exec when running java in .bat

P: 2
hey,
in my code i'm using Runtime.getRuntime().exec() in order to run a .bat file that calls another java program (they communicate with each other using RMI).
when i call:
Process process = Runtime.getRuntime().exec("cmd /c start C:\\MyFolder\\JavaApp.bat");
the seperate process runs perfectly, but when i add a space to the path:
Process process = Runtime.getRuntime().exec("cmd /c start \"C:\\My Folder\\JavaApp.bat\"");
then the java.exe refuses to load and all i get is an empty cmd with only "C:\"...

i also tried running this line with a ProcessBuilder, but that didn't work as well.
the .bat file has to be under C:\Program Files, so moving it under a folder without a space in its path is not an option.

does anyone know how can i pass through this problem?
thnx.
Nov 9 '09 #1
Share this Question
Share on Google+
3 Replies


Expert 100+
P: 785
The "C:\\MyFolder\\JavaApp.bat" is one of the arguments of the command "cmd", so if you put a space there inside the name it is parsed as 2 arguments instead of 1. I am not 100% sure but in this case if I remember right you can use method "Process exec(String[] cmdarray)" of class Runtime, and not "Process exec(String command)"! Just put the command and the arguments all as separate strings into a String array and pass that.
Nov 9 '09 #2

P: 2
i tried this method already and it didn't work.
notice i wrote \"C:\\My Folder\\JavaApp.bat\" which means it's supposed to be read by java as one argument.
by the way if i have a .bat file that contains:
"start /b notpad
start /b calc
start /b java -cp args..."
and i run it like this:
Process process = Runtime.getRuntime().exec("\"C:\\My Folder\\JavaApp.bat\"");
then the notpad and calc run (so it means that the .bat is being launched) but the java doesn't...
Nov 9 '09 #3

Expert 100+
P: 785
So if the .bat is being launched, which you can verify by the calculator pop-up, then you have solved your Java problem with Runtime.exec().
Now you need to solve a second problem, ms-dos related: how to start a java program from a batch file.
Can you please list full command instead of only ellipis (...) and "args", I mean, the real command contained inside your *.bat file instead of an informative "java -cp args...".
Did you already try this line exactly from the command line directly?
if yes, have you used environment variables before that, which are not set if the *.bat is invoked from your Java application? I mean something like CLASSPATH or PATH?
If yes, then set them inside your *.bat file or pass them along with the Runtime.exec() method.

By the way,
starting a cmd inside a cmd, that means a second shell (from your command)inside a first shell (from Java), which you tried in the first version of your posting, is unnecessary and a bad idea. I remember a little that you must use option K, that means "cmd /K ..." to pass arguments to the second cmd.
Also I remember some differences between "command" and "cmd". Just try "command" to see if they matter.
Nov 10 '09 #4

Post your reply

Sign in to post your reply or Sign up for a free account.