3

I'm trying to run a .bat file from my Java app. I've tried all the methods I could find, but none seems to work.

The problem is that the path to the .bat file containing spaces.

I'm using this method now so I can see the results in my Eclipse console

My actual code is:

Runtime rt = Runtime.getRuntime();
String processString = "cmd /c \"" + homeFolder.getAbsolutePath() + SETUP_FILE + "\" \"" + homeFolder.getAbsolutePath() + "\"";
    try {
        Process proc = rt.exec(processString);
    ...
    }

I've tried with escaping the quotes, without escaping quotes, separating the string into String[] and placing each space separated command its own cell:

{ "cmd", "/c", \"" + homeFolder.getAbsolutePath() + SETUP_FILE + "\" ... };

Again, with and without escaping the quotes: nothing works.

I also tried hard-coding the paths to both the array and the string. Same results every time.

homeFolder = C:\Users\La bla bla\workspace\ToolMaker\bin\
SETUP_FILE = setup.bat

The whole command is this:

cmd /c "C:\Users\La bla bla\workspace\ToolMaker\bin\setup.bat" "C:\Users\La bla bla\workspace\ToolMaker\bin"

Again, with or without quotes, same output:

Output: 
Error: 'C:\Users\La' is not recognized as an internal or external command,operable program or batch file.

Obviously I'm running on Windows (7, 64 bit professional). Java 7

I saw a few people said they had this issues before, but I couldn't find an answer on how to get around that.

2
  • have you tried to encode space character? Commented Aug 6, 2012 at 0:22
  • I've tried to encode the entire string using UTF-8. not sure if that's what u mean Commented Aug 6, 2012 at 0:24

2 Answers 2

10

Use the version of Runtime.exec(String[]) that takes a String[]:

Runtime rt = Runtime.getRuntime();
String[] processCommand = { "cmd", "/c", path };

try
{
    Process proc = rt.exec(processCommand);
    // ...
}

This works for me (Win7):

Runtime rt = Runtime.getRuntime();
String[] processCommand = { "cmd", "/c", "c:" + File.separatorChar + "dir with spaces" + File.separatorChar + "test.bat" };

System.out.println("xPATH: " + processCommand[2]);

Process p = rt.exec(processCommand);
// output of the command is as expected

This also works if I use \ explicitly:

String[] processCommand = { "cmd", "/c", "c:\\dir with spaces\\test.bat" };
Sign up to request clarification or add additional context in comments.

3 Comments

Really, I just did this test without issue Process proc = Runtime.getRuntime().exec(new String[]{"cmd", "/c", "dir", "C:/Program Files (x86)"});
I copied and pasted your code and for some reason it worked... though, if i'm adding "runas", "/user:administrator" ,.. to the array, it no longer works. and I need to run the batch as an administrator
Never mind. I got the order wrong.. though it still asks me for a password... obviously.
0

I had a similar issue, but I was using the start parameter, so it might not be "exactly" the same problem.

cmd doesn't like extended directory names (I don't know why personally, I just know it doesn't). It will want the "shortened" (8.2) names instead

That means wallpaper.jpg needs to become WALLPA~1.JPG

Here, you run into a problem. What happens if you have a number of wallpaper*.* files, which one do you want??

Now, to get this to work under windows properly, you're going to have to get down to the native level.

There's a Windows function called GetShortPathName (and variants of) which basically given a "long name" (path and file name) will generate the "short name" for you.

1 Comment

When I copy and paste the whole command into cmd, it works.. I'll look into this function.

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.