469,613 Members | 1,204 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,613 developers. It's quick & easy.

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

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?
Nov 9 '09 #1
3 6638
785 Expert 512MB
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
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
785 Expert 512MB
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.

Similar topics

4 posts views Thread by Hal Vaughan | last post: by
5 posts views Thread by Ayesha Ahsan | last post: by
2 posts views Thread by uwnewsgroup | last post: by
1 post views Thread by maya2000 | last post: by
2 posts views Thread by yaha1982 | last post: by
2 posts views Thread by jerry chapman | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.