43

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!

sol_var
  • 847
  • 1
  • 9
  • 9
  • *"Is there a way on Windows to run a JAR file using a JRE located in a specific folder?"* Why? And why a specific folder rather than a specific version of the JRE? – Andrew Thompson May 25 '12 at 16:10

5 Answers5

78

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.

FlightOfStairs
  • 838
  • 7
  • 7
  • 11
    See also the `javaw` variant of the [java command](http://docs.oracle.com/javase/7/docs/technotes/tools/windows/java.html). *"The **javaw** command is identical to **java**, except that with **javaw** there is no associated console window. Use **javaw** when you don't want a command prompt window to appear. The **javaw** launcher will, however, display a dialog box with error information if a launch fails for some reason."* – Andrew Thompson May 25 '12 at 16:14
15

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.

Hrobky
  • 892
  • 9
  • 13
5

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
MD XF
  • 7,860
  • 7
  • 40
  • 71
4

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.

naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
0

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

kentcdodds
  • 27,113
  • 32
  • 108
  • 187