Using Spaces when executing UNIX commands?

Hey guys –

I’m trying to execute an external program from within my Java code, which I’ve decided to go using Runtime.exec(). I have it working, but if there are any spaces involved at all in the application name or path to the application things just do not work.

This particular example is the Mac OS X version of the code (I have different ones for Mac, PC, and Linux), and is being shown here because it’s the only platform I have tested so far.

If I want to run something called “Test.app” that is in my users folder, I would do it like this:
Runtime.getRuntime().exec(“open /Users/eli/Test.app”); <—Works fine

That works perfectly, executing the app with no issue. However, if I want to execute “Test App.app” then nothing happens. The processor makes the “doing something” noise as if it is computing, but no programs open, and no error messages appear.
Runtime.getRuntime().exec(“open /Users/eli/Test App.app”); <— Does nothing
Runtime.getRuntime().exec(“open /Users/eli/“Test App.app””); <— Does nothing
Runtime.getRuntime().exec(“open /Users/eli/Test\ App.app”); <— Does nothing
Runtime.getRuntime().exec(“open /Users/eli/Test*App.app”); <— Does nothing
Runtime.getRuntime().exec(“open /Users/eli/Test?App.app”); <— Does nothing

Spaces seem to bring it down, both in directories leading to the app, and in the app’s name. Is there some special way to represent spaces that I am not aware of? If not, I suppose I can have the user rename the app beforehand, but that just seems stupid. It should be noted that I have tried all of the above 5 attemps within my Terminal app (the UNIX shell for Mac OS X), and every single one works correctly, except the first of course. Surrounding it in quotes makes UNIX ignore everything within (aka the space), putting a backslash before the space means “canceling out” the character following (much like \n in Java), putting in a * allows for absolutely any character, and putting in a ? allows for anything except nothing… so, they should all work, and do, just not when I’m doing these commands from within Java. Any ideas?

Thanks.

Runtime.getRuntime().exec("open \"/Users/eli/Test App.app\"");

That should do the job, but I would have expected Runtime.getRuntime().exec("open /Users/eli/Test\\ App.app"); to work too.

HTH

Endolf

Yeah, neither worked, even though they should have. However, I was able to get it to work by passing an array of commands, in which case Java seems to ignore subsequent spaces.

Runtime.getRuntime().exec(new String[]{“open”,“Test App.app”},null,new File(System.getProperty(“user.dir”)));

The above works fine, for whatever reason. And using a File to have it start in the correct directory seems to help a lot as well.