I am shipping a executable jar file to customer, Customer has installed JRE 5, JRE 6 and JRE 7 on the box. My Jar required JRE 7 to run. Without changing the system PATH (Environment var) how can I specify the JRE 7 to use?
-
String version=System.getProperty("java.version"); if(!version.StartsWith("1.7")){ // open download page to java } – Anirudha Feb 03 '15 at 12:26
-
thanks but this is not the requirement – user1493834 Feb 03 '15 at 13:47
-
1Above **answer** to only check for the version and only to continue if it is starting with 1.7 locks you to using a specific Java version and is therefore absolutely wrong. – Maarten Bodewes Feb 05 '15 at 11:07
-
It seems like you are trying to take responsibility from the system admin. Basically you shouldn't. If you just want to use the latest version, ask the system admin to make sure the system is setup to use the *latest stable release* of Java by default. JRE5 and 6 are already deprecated, and if your application is well written then it will run fine on JRE 8. Not an answer, I know from experience this is not always possible. – Maarten Bodewes Feb 05 '15 at 12:29
3 Answers
You can specify full path to that JRE that you need, for example:
/path/to/jre/bin/java.exe -jar executable.jar
or
/path/to/jre/bin/javaw.exe -jar executable.jar
If you run this from a shell (script) then it is good practice to first set the JAVA_HOME
environment variable to the right location before (/path/to/jre
) before running the executable. You could first set/export JAVA_HOME
and then extend it to the location of the Java executable (e.g. %JAVA_HOME%\bin\java.exe
on Windows). More information here.
-
Beware that this is a very dangerous path to take. Usually the latest version of a runtime is in jre7 or jre8. This will receive updates as it is a patch-in-place installation (as Oracle calls it). If you are pointing to a static runtime or if the location does not receive (security) updates anymore, you may run into trouble. – Maarten Bodewes Feb 05 '15 at 12:17
I'm not sure there's a cross-platform way to achieve this.
On Windows you can use a tool such as launch4j to wrap up the jar as a .exe
that can select an appropriate JRE.
On Mac OS X you can have several different JDKs installed in parallel but only one public JRE (which will be at least the latest version out of the installed JDKs, and may be newer if it's been auto updated). It's the public JRE that is used for app bundles and when double clicking a JAR in finder.

- 120,891
- 16
- 170
- 183
-
Usually such launchers have specific methods to find the best JRE to use. So this answer should be preferred over directly specifying the VM executable (e.g. `java.exe`) as it is more flexible and possibly more secure. It does however add another component to your runtime. – Maarten Bodewes Feb 05 '15 at 12:32
The simplest way to do this, is to use Java WebStart to launch your program and then specify you need a suitably new version of Java. The launcher will then locate a suitable distribution on your system to use.
Using javaws also allows you to easily distribute new updates to your users.
Caveat: Caching has notoriously been a problem over the years. Ensure that when a jar changes content, its URL changes too.

- 73,784
- 33
- 194
- 347