Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java command Runtime.getRuntime().exec() in Mac OS

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.

like image 294
user897013 Avatar asked Nov 02 '11 11:11

user897013


People also ask

What is runtime getRuntime () exec?

In Java, the Runtime class is used to interact with Every Java application that has a single instance of class Runtime that allows the application to interface with the environment in which the application is running. The current runtime can be obtained from the getRuntime() method.

What is exec method in Java?

exec(String[] cmdarray, String[] envp) method executes the specified command and arguments in a separate process with the specified environment. This is a convenience method. An invocation of the form exec(cmdarray, envp) behaves in exactly the same way as the invocation exec(cmdarray, envp, null).

How do you find a running Java process on Macos Linux terminal console )?

On Linux, you can view processes with the ps command. It is the simplest way to view the running processes on your system. You can use the ps command to view running Java processes on a system also by piping output to grep .


2 Answers

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

like image 75
beny23 Avatar answered Sep 17 '22 12:09

beny23


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
like image 22
Nicolas Modrzyk Avatar answered Sep 21 '22 12:09

Nicolas Modrzyk