Samantha Catania Samantha Catania - 7 months ago 31
Java Question

Running Java Program Multiple Times on Mac

Q. Is there a way to have a java program run twice on a mac like on windows?

Answer

You do not specify if you want to start a simple .jar or an application-bundle so I will give examples for both. To run multiple instances of an application-bundle on OS X, you can use the following trick; open the Terminal and start the application with this command:

open -n /path/to/your/java.app

Each time you call this command, a new instance is opened up.

Attention: Just because it is possible to start several instances does not mean it is a good idea to do so. Make sure you will not run into trouble with concurrent write-access of multiple instances with the same file.

If you are trying to run a jar, you can simply call

java -jar /path/to/your/java.jar

several times to start up several instances.

To start up the java-application from inside a java-application under OSX, you have to do something like this:

In the case of a simple jar:

File jarFile = new File("/path/to/your/jarFile.jar");
Runtime.getRuntime().exec(new String[] { "java", "-jar", jarFile.getAbsolutePath() });

In the case of an application bundle:

File jarFile = new File("/path/to/your/jarFile.app");
final String[] command = { "open", "-n", jarFile.getAbsolutePath() };
Runtime.getRuntime().exec(command);