tags:

views:

39

answers:

2

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

+1  A: 

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

GHad
I did follow the instruction. However when I go to run the code, the DOS command window appears with the cursor flashing at C:\Documents and Settings\Zatko\My Documents\Project-Workspace\IUG\>
Tony
Check if your server has been started already. May the batch file starts a process which leaves the command window open. When I try with a batch containing only the pause command, it works perfectly here.
GHad
No, the server is not started...
Tony
Do you need to change the / to \ as well?
MatthieuF
It seems that the problem is indeed in the path: any batch file if located in the main C:/ folder is executed without problem. However if the same file is located in the C:/Documents and Settings/Zatko/My Documents/Project-Workspace/IUG/external/apps/archive/ simply doesn't work...
Tony
+1  A: 

It's much better to use an array:

String[] array = { ... };
Runtime.getRuntime().exec(array);
Thomas Mueller