Click here to Skip to main content
15,885,956 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all,
I have created a nice java application.
What I want to do now, is create a separate application, that allows you to select a jar file, from your filesystem, and then press a button, and open it.

I want the application to work on Mac, and PC.

Now I know the ability to click and open is available with Swing applications, however, console applications cannot be double clicked to open (AFAIK).

I also know that I can create a *.bat (Windows) or *.sh (Mac) file that opens to jar, but I want to practice Java some more, and would like to learn how to do this.

Thank you all,

Gigabyte Giant
Posted

1 solution

On windows there are two executables in the jre to run java applications: java and javaw. The main differenc is that java is a console application while javaw isn't, it is usually used to execute gui applications. If you click a jar file in windows then usually javaw is started because the java installer associates jar files with the "javaw -jar %1" command where %1 is the clicked jar file.

Running a jar file with console:
java -jar jarfile

without console:
javaw -jar jarfile


Detecting the location of the java or javaw programs can be tricky, probably one crossplatform way is looking for the JAVA_HOME env var but it isn't necessarily set. On the other hand a machine can have several versions of the jre installed, most java development IDEs ask you to select one of them for your project. I don't know how to detect all of them and this can be platform specific, see google if you are interested about this...

If your launcher program is also a java application then you can find out the directory of the jre by looking at the environment variables of your running launcher application. For example:
Java
String jre_dir = System.getProperty("java.home");

Then you can run the console version of the java program from this jre folder to execute the selected jar file (with the "jre_dir/bin/java -jar your_jar" command).
 
Share this answer
 
Comments
Gigabyte Giant 27-Jan-14 19:25pm    
Thanks, this definitely helped! +5
pasztorpisti 27-Jan-14 20:31pm    
You are welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900