Reputation: 4305
I am trying to execute a batch file in my Java application. The code is the following:
Runtime.getRuntime().exec("cmd /C start C:/Documents and Settings/Zatko/My Documents/Project-Workspace/IUG/external/apps/archive/run-server.bat");
When it executes, a error dialog appear telling "Windows cannot find 'C:/Documents'. make sure you typed the name corretly...."
When I execute with the same code another batch file, named file.bat and located in the C:/Temp folder, it works perfectly....
Does anyone know where the problem may be? Is it about spacing characters?
Thanks in advance
Upvotes: 0
Views: 3587
Reputation: 354854
Runtime.getRuntime().exec("cmd /C start \"\" \"C:/Documents and Settings/Zatko/My Documents/Project-Workspace/IUG/external/apps/archive/run-server.bat\"");
should work.
You need to quote arguments with spaces or shell metacharacters in them. And start
expects the first quoted argument to be a window title, so give it an empty one so it's happy.
Upvotes: 2
Reputation: 13734
This works :
List<String> templst = new ArrayList<String>();
templst.add("cmd");
templst.add("/C");
templst.add("start");
templst.add("backup.bat");
Process p = rt.exec(templst.toArray(new String[]{}), null, new File(path));
Upvotes: 0
Reputation: 50147
It's much better to use an array:
String[] array = { ... };
Runtime.getRuntime().exec(array);
as in
String[] array = { "cmd", "/C", "start",
"C:/Documents and Settings/Zatko/My Documents/.../run-server.bat" };
Runtime.getRuntime().exec(array);
Using an array is specially important if you have spaces in one of the parameters, like you do.
Upvotes: 2
Reputation: 9641
Edit:
It seems the start command needs an extra parameter whenever the path to the executable to start is enclosed in ". As one must surround parameters which contains spaces by " this is a little bit confusing as the start comand works as excepted when one has a path without spaces and thus does not enclose it with ". That's what happened when I tested the code below for a folder c:/temp and it worked without an additional parameter.
The parameter in charge is a title for the window that is opened. It must come es second paramter and if it contains spaces must be surrounded by ".
I suggest to always use " for both title and path.
So here is the updated command:
You need to enclose
c:/Document and Settings/...
with " as the filename contains spaces. And you need to include a title when using the start command with a paramter with ".
For Java that would be:
Runtime.getRuntime().exec("cmd /C start \"Server\" \"C:/Documents and Settings/Zatko/My Documents/Project-Workspace/IUG/external/apps/archive/run-server.bat\"");
Greetz, GHad
Upvotes: 2