5

I´m using Mac OS Lion, with java version 1.6.0_26

I'm making a small app for Mac in Java with a main menu for the user, so he can choose several options.

One of them is install an app using a .pkg

Everything was working fine with these commands:

File instFolder = new File(System.getProperty("user.dir") + "/foldername/appInstaller.pkg");
String s = "open "+ instFolder.toString();
Process p = Runtime.getRuntime().exec(s);

Then I realized that there is a problem when foldername has spaces or if I copy this java file with the needed subfolders to a USB pen drive with "NO NAME" as name (or some name with spaces).

Because s will become something like:

open /Volumes/NO NAME/foldername/appInstaller.pkg
or
open /Users/user1/Desktop/folder name/appInstaller.pkg



So when you run the p process, the command will finish where the first space appears on the path

open /Volumes/NO
or
open /Users/user1/Desktop/folder


To try to fix this I changed the s definition for something like this:

String s = "open "+ "\"" + instFolder.toString() + "\"";

It stopped working fine. The strange thing is that if i copy the s value (after creating the s variable) and paste it in the terminal it works:

open "/Users/user1/Desktop/folder name/appInstaller.pkg"


but running it from Java it does't work.

Could you help me, please?

Thanks.

user897013
  • 165
  • 1
  • 3
  • 9

3 Answers3

6

In order to properly escape arguments, you can use the following:

Runtime.getRuntime().exec(new String[] { "open", instFolder.toString() });

Though I would probably to use the more modern ProcessBuilder:

ProcessBuilder pb = new ProcessBuilder("open", instFolder.toString());
Process p = pb.start();
int exitCode = p.waitFor();

Though this may be worth a read depending on what you want to do with the processes output.

Note: edited to reflect question in comment

Community
  • 1
  • 1
beny23
  • 34,390
  • 5
  • 82
  • 85
  • Hi @beny23: Thanks for yours suggestions. They both worked well, but i have a question. Let supose that the instFolder is wrong so when i try to run the installer the process will return a exitcode<>0 so i can know that an error has ocurred. How can i check that with process builder? – user897013 Nov 02 '11 at 15:28
  • The `Runtime.exec` method actually uses `ProcessBuilder`, so I wouldn't say one is "more modern" than the other. – Jenna Sloan Feb 10 '19 at 06:51
  • Runtime.exec since Java 1.0 - ProcessBuilder since 1.5 – beny23 Jul 24 '21 at 11:55
2

it seems your path does not have quotes when turned into the shell.

You should probably add "'" on both sides of your path, so the final shell command will look like:

open 'your path' 

instead of

open your path
Nicolas Modrzyk
  • 13,961
  • 2
  • 36
  • 40
0

Here's a little trick that came out from the answers mentioned above:

ProcessBuilder pb = new ProcessBuilder(commandString.split(" "));

Say commandString = "killall Mail" then the split will separate the words making it a String[] parameter to the ProcessBuilder.

didinino
  • 93
  • 1
  • 8