Want to highlight a helpful answer? Upvote!

Did someone help you, or did an answer or User Tip resolve your issue? Upvote by selecting the upvote arrow. Your feedback helps others! Learn more about when to upvote >

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Java: exec(): execute an app with whitespaces in path?

Hi there,
I use the java exec() command to execute some commands on my unix.
The problem is, that I cannot exec Applications, which have a path with whitespaces inside. A few examples:

This works:
"/Volumes/WD120/folder/App"

This doesnt work:
"/Volumes/WD120/some folder/App"
"/Volumes/WD120/some\ folder/App"
"/Volumes/WD120/some\\ folder/App"

Does anyone have an idea how this can be done? To escape them doesnt work and actually, there has to be some way, right?
Thanks alot, I really need help on this, google doesn't give me anything helpful 😟
-Lucas

Posted on Sep 19, 2005 2:53 AM

Reply
Question marked as Best reply

Posted on Sep 21, 2005 12:43 PM

Does no one know how to do this?

b I use Runtime.getRuntime().exec("/path with/whitespaces/inside/Application");

It does not work 😟 Does anybody know why?
No even if I escape the whitespace. This doesn't work aswell:

b I use Runtime.getRuntime().exec("'/path with/whitespaces/inside/Application'");
14 replies
Question marked as Best reply

Sep 21, 2005 12:43 PM in response to Lucas P

Does no one know how to do this?

b I use Runtime.getRuntime().exec("/path with/whitespaces/inside/Application");

It does not work 😟 Does anybody know why?
No even if I escape the whitespace. This doesn't work aswell:

b I use Runtime.getRuntime().exec("'/path with/whitespaces/inside/Application'");

Sep 21, 2005 8:07 PM in response to Lucas P

Runtime.exec is very finicky in Java ... it doesnt actually run the command line interpreter the way one would think. You need to use a parameter array when you have spaces in the path. You also need to issue the command to launch a app which is 'open' in OSX:

String [] cmdArray = new String[2];
cmdArray[0] = "open";
cmdArray[1] = "/Users/auser/Applications/Folder with space/SomeApplication.app";

Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(cmdArray);

Sep 22, 2005 10:18 AM in response to Ben Martell

Hi Ben!
Thanks alot, this worked:
--------------------------------------------------
String [] cmdArray = new String[2];
cmdArray[0] = "open";
cmdArray[1] = "/Users/auser/Applications/Folder with space/SomeApplication.app";
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(cmdArray);
--------------------------------------------------
but what I didnt figure is, how to give the app parameters...
cmdArray[0] = "open";
cmdArray[1] = "/Users/auser/Applications/Folder with space/
cmdArray[2] = "-opti=test";
If I have the parameter "-opti=test", how can I hope the app with open AND giving the app a parameter?
Thanks alot for your support!
-Lucas

Sep 22, 2005 10:21 AM in response to Lucas P

Generally you wouldn't want to pass arguments to a GUI application. There is no mechanism for doing so in Launch Services, which is the recommended API for launching them.

If it is a Mach-O binary, then you can execute the UNIX executable inside the application package directly:
cmdArray[0] = "/path/to/application.app/Contents/MacOS/application";
cmdArray[1] = "-opt=test";

Or something along those lines.

Sep 22, 2005 6:57 PM in response to Lucas P

Based on the syntax of your post here It looks like you are trying to place the parameters between the path and the application. You want to place the parameters last.

cmdArray[0] = "open";
cmdArray[1] = "/Users/auser/Applications/Folder with space/SomeApp.app
cmdArray[2] = "-opti=test";

Also a very good way to figure this out is to open terminal and figure out how to launch the application from there. Once you have that working it is trivial to place the parameters in your java program.

If the application you are attempting to launch is freeware or shareware, post the information and I will be glad to show you the specifics for it.

Sep 22, 2005 11:46 PM in response to Finlay

You are right Finlay. My mistake. The following is an example of syntax that works for passing a file to an application as an argument:

String [] cmdArray = new String[2];
cmdArray[0] = "/Applications/TextEdit.app/Contents/MacOS/TextEdit";
cmdArray[1] = "/Users/Shared/Untitled.rtf";

Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(cmdArray);

If the application he is trying to launch accepts parameters and can be launched from within the package it should work for him. This works with or without spaces in the appliation path name for me.

Sep 23, 2005 1:52 AM in response to Ben Martell

Yes, parameters didn't work with the open command....
The application is inside the .app package:

b /path/with spaces/appbundle.app/Contents/MacOS/application

Thats the path. The parameter is "-param=value". You say this works for you?:

String [] cmdArray = new String[2];
cmdArray[0]= "/Applications/TextEdit.app/Contents/MacOS/TextEdit";
cmdArray[1] = "/Users/Shared/Untitled.rtf";
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(cmdArray);

This works even with spaces in paths? It does not for me.... Actually that application / Document thing is different than patrameters. "open" does allow the docu as a parameter...
Still no solution as it seems 😟. But thanks alot for all your time. Great place!
-Lucas

Sep 23, 2005 9:09 AM in response to Lucas P

Very interesting.

I just built a small app that will take a parameter on launch and that will work for me also (with spaces or not).

You say that you cant launch the exact code I pasted in (to launch TextEdit) with spaces in the path? Hmmm.

What version of java are you linking to and what version of OSX are you running? What type of application are you trying to launch (Cocao, Carbon, Java, etc)? Can you launch it from the command line in terminal with the parameters?

You have something strange going on here.

Oct 9, 2005 8:45 AM in response to Maximillian Murphy

After many tests with the exec() command, I have been looking for a solution, to get a signal when my exec() command has been finished.
For example:

1) I launch my application (App1)
2) It does exec some other app (App2) which appears
3) The user can do something there
4) When the user closes App2:
5) App1 (my own app) should get a signal that App2 now is closed

I have tried some things with the unix commands "top" and "ps" ("ps -aux"). But I didnt have many luck... Is there a java solution for this problem? Has anyone of you guys done this before? Thanks alot for any comment on this prob.
-Lucas

Java: exec(): execute an app with whitespaces in path?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.