Run a JAR file using a specific JRE

Is there a way on Windows to run a JAR file using a JRE located in a specific folder? Similar to the way Eclipse looks for its JRE in some path you give to it. Either some windows executable code (C or C++) or a Batch file will do the job. Thanks!

Answers


A JRE directory has a bin/java.exe.

You can run a jar from that JRE simply with

<path_to_jre>/bin/java.exe -jar Executable.jar

If you don't want to have to open a console each time, simply put the above line in a .bat file and double click on that.


Create this batch file in the same folder as your jarfile:

@echo off
set path=C:\Program Files (x86)\java\bin\;%path%
java -version
javaw -jar jaryouwanttorun.jar
pause
exit

Run from command
<path_to_jre>/bin/java.exe -jar Executable.jar

(or use javaw.exe to return to command prompt immediately after launch of the JAR)

Or set environment variables

Many start scripts respect variables JRE_HOME and JAVA_HOME for JRE and JDK respectively. Some don't like spaces, so use short path convention (C:\Progra~1\Java\jre1.8.0_171)

On 64bit systems: Progra~1 = Program Files Progra~2 = Program Files (x86)

Or set file associations

to launch JARs by double clicking. Unfortunately, the GUI (Control Panel\All Control Panel Items\Default Programs\Set Associations) is quite lousy, so you have to do it in Registry.

This is my favorite method to choose 32/64bit JRE, when the Control Panel setting is ignored.

  1. Check that the default value in HKEY_CLASSES_ROOT\.jar is jarfile
  2. Check that the default value in HKEY_CLASSES_ROOT\jarfile\shell\open\command points to your JRE.

In my case for 64bit env:

"C:\Program Files\Java\jre1.8.0_171\bin\javaw.exe" -jar "%1" %*

Mind the quotes: javaw path contains space; the JAR path can contain spaces; passed parameters are space separated.


You could change the Windows Environment Variable for JAVA_HOME (see here). Point it to the JRE you want it to run with. I'm sure there's no programmatic way to do it (because the right JRE is loaded at run-time).


PowerShell syntax is as follows

& 'C:\Program Files\Java\jdk1.7.0_80\bin\java.exe' -jar .\Executable.jar -Xmx256m

Use a path to specific java.exe installed in your system.


Need Your Help

Calling superclass from a subclass constructor in Java

java subclass superclass accessor

I am trying to create a constructor that takes a field as a parameter, then puts it in a field that is stored in a superclass. Here is the code I am using

UIActivityIndicatorView for Mac OS X?

cocoa macos uiactivityindicatorview

Is't there a "NSActivityIndicatorView" for Mac OS X like the UIActivityIndicatorView for iOS?