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
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.
<path_to_jre>/bin/java.exe -jar Executable.jar
(or use javaw.exe to return to command prompt immediately after launch of the JAR)
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)
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.
HKEY_CLASSES_ROOT\.jar
is jarfile
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.
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.
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).
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