Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to specify JRE to run a executable jar

Tags:

java

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?

like image 651
user1493834 Avatar asked Feb 03 '15 12:02

user1493834


People also ask

How do I run a jar file in JRE?

Java Runtime Environment (To Run The file) If you want to run the JAR file, you will need the Java Runtime Environment. If you have the Java Runtime Environment, then all you need to do is to double click on the file name. But it will only work if that particular file is executable.

Is JRE required to run jar?

You need a JRE but not the JDK. The JRE is the java runtime environment and java code cannot be executed without it. The . jar is a compiled java file can and this needs the java runtime environment to be run.

Can I run jar file without installing JDK or JRE?

No, you can't. To run an executable jar you need a JRE. Current best practices for distributing Java applications is to bundle a compatible JRE with your application. The JDK tools jink and jpackage help create JRE images and native installers with executables that will launch the embedded JRE and run the Java code.


2 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.

like image 101
Taras B Avatar answered Sep 29 '22 22:09

Taras B


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.

like image 36
Ian Roberts Avatar answered Sep 29 '22 20:09

Ian Roberts