La bla bla
La bla bla

Reputation: 8708

Java run command line which contains spaces

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.

Upvotes: 3

Views: 8542

Answers (2)

pb2q
pb2q

Reputation: 59607

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" };

Upvotes: 10

MadProgrammer
MadProgrammer

Reputation: 347184

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.

Upvotes: 0

Related Questions