0

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

4 Answers 4

2

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

Sign up to request clarification or add additional context in comments.

4 Comments

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\>
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.
Do you need to change the / to \ as well?
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...
2

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.

3 Comments

can u give an example of using it in the above situation?
I certainly thought the same thing and tried it, but it doesn't work! It only opens up a command window with current folder as the project root (same as mentioned in tony's comment above)
I got the reason, check out my answer below
2
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.

Comments

0

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));

1 Comment

don't try to use /b flag, it doesn't work in that case. Instead simply write exit as the last command in your batch file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.