Setting the JVM via the command line on Windows Setting the JVM via the command line on Windows windows windows

Setting the JVM via the command line on Windows


Yes - just explicitly provide the path to java.exe. For instance:

c:\Users\Jon\Test>"c:\Program Files\java\jdk1.6.0_03\bin\java.exe" -versionjava version "1.6.0_03"Java(TM) SE Runtime Environment (build 1.6.0_03-b05)Java HotSpot(TM) Client VM (build 1.6.0_03-b05, mixed mode, sharing)c:\Users\Jon\Test>"c:\Program Files\java\jdk1.6.0_12\bin\java.exe" -versionjava version "1.6.0_12"Java(TM) SE Runtime Environment (build 1.6.0_12-b04)Java HotSpot(TM) Client VM (build 11.2-b01, mixed mode, sharing)

The easiest way to do this for a running command shell is something like:

set PATH=c:\Program Files\Java\jdk1.6.0_03\bin;%PATH%

For example, here's a complete session showing my default JVM, then the change to the path, then the new one:

c:\Users\Jon\Test>java -versionjava version "1.6.0_12"Java(TM) SE Runtime Environment (build 1.6.0_12-b04)Java HotSpot(TM) Client VM (build 11.2-b01, mixed mode, sharing)c:\Users\Jon\Test>set PATH=c:\Program Files\Java\jdk1.6.0_03\bin;%PATH%c:\Users\Jon\Test>java -versionjava version "1.6.0_03"Java(TM) SE Runtime Environment (build 1.6.0_03-b05)Java HotSpot(TM) Client VM (build 1.6.0_03-b05, mixed mode, sharing)

This won't change programs which explicitly use JAVA_HOME though.

Note that if you get the wrong directory in the path - including one that doesn't exist - you won't get any errors, it will effectively just be ignored.


You should be able to do this via the command line arguments, assuming these are Sun VMs installed using the usual Windows InstallShield mechanisms with the JVM finder EXE in system32.

Type java -help for the options. In particular, see:

-version:<value>              require the specified version to run-jre-restrict-search | -jre-no-restrict-search              include/exclude user private JREs in the version search


yes I often need to have 3 or more JVM's installed. For example, I've noticed that sometimes the JRE is slightly different to the JDK version of the JRE.

My go to solution on Windows for a bit of 'packaging' is something like this:

@echo offsetlocal@rem  _________________________@rem@set  JAVA_HOME=b:\lang\java\jdk\v1.6\u45\x64\jre@rem@set  JAVA_EXE=%JAVA_HOME%\bin\java@set  VER=test@set  WRK=%~d0%~p0%VER%@rem@pushd %WRK%cd @echo.@echo  %JAVA_EXE%  -jar %WRK%\openmrs-standalone.jar       %JAVA_EXE%  -jar %WRK%\openmrs-standalone.jar @rem@rem  _________________________popdendlocal@exit /b

I think it is straightforward. The main thing is the setlocal and endlocal give your app a "personal environment" for what ever it does -- even if there's other programs to run.