You may create 2 batch file one for java 7 and one for java 8 like this -
jdk7.bat
@echo off
echo Setting JAVA_HOME
set JAVA_HOME=C:\Program Files\Java\jdk1.7.0_11
echo setting PATH
set PATH=C:\Program Files\Java\jdk1.7.0_11\bin;%PATH%
echo Display java version
java -version
jdk8.bat
@echo off
echo Setting JAVA_HOME
set JAVA_HOME=C:\Program Files\Java\jdk1.8.0_11
echo setting PATH
set PATH=C:\Program Files\Java\jdk1.7.8_11\bin;%PATH%
echo Display java version
java -version
You may quickly switch between them running these batch file.
Answer from Razib on Stack OverflowYou may create 2 batch file one for java 7 and one for java 8 like this -
jdk7.bat
@echo off
echo Setting JAVA_HOME
set JAVA_HOME=C:\Program Files\Java\jdk1.7.0_11
echo setting PATH
set PATH=C:\Program Files\Java\jdk1.7.0_11\bin;%PATH%
echo Display java version
java -version
jdk8.bat
@echo off
echo Setting JAVA_HOME
set JAVA_HOME=C:\Program Files\Java\jdk1.8.0_11
echo setting PATH
set PATH=C:\Program Files\Java\jdk1.7.8_11\bin;%PATH%
echo Display java version
java -version
You may quickly switch between them running these batch file.
If the program uses a batch to start, then add this line before the start of the program:
SET JAVA_HOME="C:\Program Files\Java7\Java.exe"
(This is just an example, the directory might be different on your computer)
If the program does not use such a batch (you can recognize it because it ends with either .cmd or .bat) create such a file and use that for launching the program instead:
@echo off
SET JAVA_HOME=...
ThisIsMyFancyScalaProgram.Exe
How do I set up a path variable for the java command javac?
How to set java.library.path from command line?
Setting JAVA_HOME path
Running Java in Command Prompt?
Videos
It doesn't change value in path or java -version doesn't change in current instance of cmd
You have to restart cmd for the changes to take effect as a cmd shell inherits it environment from the parent process.
So what is the correct way to switch between Java versions from the command line?
Use a set of batch files, as follows:
Being a Java developer, I always compile and test my code on different Java versions. But switching between them is a huge problem. So finally I found an easy method to do this. You have to create following batch files and place them in directory you open your command line in or in SYSTEM PATH. You can use you favorite text editor to create these files.
jdk14.bat
@echo off echo Setting JAVA_HOME set JAVA_HOME=C:\j2sdk1.4.2_12 echo setting PATH set PATH=C:\j2sdk1.4.2_12\bin;%PATH% echo Display java version java -versionjdk15.bat
@echo off echo Setting JAVA_HOME set JAVA_HOME=C:\Program Files\Java\jdk1.5.0_12 echo setting PATH set PATH=C:\Program Files\Java\jdk1.5.0_12\bin;%PATH% echo Display java version java -versionjdk16.bat
@echo off echo Setting JAVA_HOME set JAVA_HOME=C:\Program Files\Java\jdk1.6.0_11 echo setting PATH set PATH=C:\Program Files\Java\jdk1.6.0_11\bin;%PATH% echo Display java version java -versionMake sure you assign the appropriate
JAVA_HOMEvalue in batch files, according to your Java installation. Whenever you want to switch between Java versions, just run the respective batch file and you are done.Note:
JAVA_HOMEand the path to java must always refer to the exact same version of theJDK. If you mix them up, unpredictable things will happen!
Source Switch between different JDK versions in Windows | Oracle Pranav's Blog
The reason for this is that the variable reference in PATH is expanded at the time of the assignment to PATH, any later changes are ignored, the reference to the original value is lost. It's like making a copy of the value of the variable, not creating a reference to the variable.
set JAVA_HOME=C:\dir1
PATH=%JAVA_HOME%
set JAVA_HOME=C:\dir2
PATH
This will output C:\dir1 (value of JAVA_HOME at the time of the assignment) and not %JAVA_HOME%.
You need a script like this to call after a change to JAVA_HOME:
PATH=%JAVA_HOME%;C:\Windows\system32;C:\Windows;...
Is it possible to set jar for java.library.path?
Following command will set the JAVA_HOME environment variable. This is required if you are to install java based application such as Eclipse,Tomcat etc
SET JAVA_HOME = C:/Program Files/Java
Following command will set the PATH environment variable. This is required if you want to access java compiler(javac) and java itself.
SET PATH = %PATH%;%JAVA_HOME%\bin
How about this :
set JAVA=path\to\the\desired\jdk
set PATH=%JAVA%;%PATH%
But remember that everytime you change/create an env variable, you'll see these changes only in the current cmd.exe opened.
If you want to change your jdk system wide thanks to a script, I'm not sure this is so easy on Windows.