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 Overflow
🌐
Oracle
java.com › en › download › help › path.html
How do I set or change the PATH system variable?
In the Edit System Variable (or New System Variable) window, specify the value of the PATH environment variable. Click OK. Close all remaining windows by clicking OK. Reopen Command prompt window, and run your java code. To run a different version of Java, either specify the full path, or use ...
Discussions

How do I set up a path variable for the java command javac?
I want to use the jdk java compiler ... as an internal or external command,operable program or batch file." Java told me to change the path variable but i am not able to. The location of my Java Compiler is, "C:\Program Files\Java\jdk1.7.0" Can someone please tell me how to set the path variable ... More on learn.microsoft.com
🌐 learn.microsoft.com
2
0
How to set java.library.path from command line?
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/javahelp
10
1
December 4, 2021
Setting JAVA_HOME path
It means that you have not configured the JAVA_HOME environment variable. Using a virtual terminal application, type in: update-alternatives --config java, which will give you the location of JDK listed under Path column. Depending on the installed version of JDK, it will be something like: /usr/lib/jvm/java-14-openjdk-amd64/bin/java If you have installed multiple versions of JDK, all of them will be listed here 2. Using text editor such as nano and terminal emulator, type in: nano /etc/profile.d/jdk_home.sh This will create and open for editing a new script file named jdk_home.sh under the location /etc/profile.d, where you need to type in: export JAVA_HOME= where JDK-Location should not include the /bin/java directories. For example: EXPORT JAVA_HOME=”/usr/lib/jvm/java-14-openjdk-amd64” This will set your environment variable JAVA_HOME The steps above are if you have installed JDK using apt package manager tool, you also have the option of using SDKMAN which will configure all environment variables for you automatically. More on reddit.com
🌐 r/vscode
12
1
February 27, 2020
Running Java in Command Prompt?
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/javahelp
11
1
February 2, 2021
🌐
TutorialsPoint
tutorialspoint.com › how-to-set-temporary-and-permanent-paths-in-java
How to set Temporary and Permanent Paths in Java?\\n
Write in the command prompt: SET PATH=C:\Program Files\Java\jdk_version\bin and hit enter command.
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-set-temporary-and-permanent-paths-in-java
How to Set Temporary and Permanent Paths in Java? - GeeksforGeeks
May 12, 2025 - Copy the path of jdk/bin directory (e.g. C:\Program Files\Java\jdk-24\bin) In the Command Prompt, write the following command ... Note: This command sets the path for the current prompt session and when the session is closed the path will no longer be valid.
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-set-java-path-in-windows-and-linux
How to Set Java Path in Windows and Linux? - GeeksforGeeks
July 23, 2025 - The commands java and javac are not known to the operating system as we don't specify where the executables reside. Hence, we need to specify the path where the executables are located. This is the reason we set the path and specify the path of the bin folder because the bin contains all binary executable files.
🌐
Microsoft Learn
learn.microsoft.com › en-us › answers › questions › 2495532 › how-do-i-set-up-a-path-variable-for-the-java-comma
How do I set up a path variable for the java command javac? - Microsoft Q&A
In addition, you might try the following website. Java.com: Support Options: http://www.java.com/en/download/support.jsp ... <start><control panel><system><advanced system settings> enter admin P/W if prompted <Environment variables> This gives the basic idea - http://geekswithblogs.net/renso/archive/2009/10/21/how-to-set-the-windows-path-in-windows-7.aspx - (note 'my computer' - 'computer', 'advanced' - 'Advanced system settings')
🌐
CodeJava
codejava.net › java-core › how-to-set-environment-variables-for-java-using-command-line
How to set environment variables for Java using command line
April 9, 2020 - When the cmd.exe icon appears, right click and select Run as administrator.To add/update system environment variables: setx -m JAVA_HOME "C:\Program Files\Java\jdk1.8.0" setx -m PATH "%PATH%;%JAVA_HOME%\bin";I prefer setting the environment variables using this command-line alternative.
Find elsewhere
🌐
Just Academy
justacademy.co › blog-detail › how-to-set-path-in-cmd-for-java
How to Set Path in CMD for Java by Roshan Chaturvedi | JustAcademy
15) In the System Properties window, click on ‘Advanced system settings’, then click the ‘Environment Variables’ button. 16) In the Environment Variables window, under System variables, find the ‘Path’ variable and click ‘Edit’. 17) Click ‘New’ and paste the path to the JDK bin directory copied earlier. Click ‘OK’ to save the changes. 18) Close and reopen Command Prompt to apply the changes and now you can run Java commands from any directory.
🌐
Quora
quora.com › How-do-I-set-the-path-in-Java-using-CMD
How to set the path in Java using CMD - Quora
Answer (1 of 4): Your question is not clear but do you mean path to java home directory ? Or the path variable in a Java application ? on Windows set JAVA_HOME=javahome then set “PATH=%JAVA_HOME%\bin;%PATH%” on Linux set JAVA_HOME=javahome then set PATH=$JAVA_HOME/bin:$PATH
🌐
C# Corner
c-sharpcorner.com › article › how-to-set-up-the-java-path-on-windows
How to Set Up the Java Path on Windows
December 27, 2024 - The topic "How to Set Up the Java Path on Windows" covers the essential steps needed to configure your system so that Java commands can be run from anywhere in the command line.
🌐
XDA Developers
xda-developers.com › home › windows11 › how to set the java path in windows 11
How to set the Java path in Windows 11
November 29, 2023 - In the new window that appears, click on the New button under System variables. Type in JAVA_HOME in the Variable name box. For Variable value, type in the path to your JDK folder — in this example, the path is “C:\Program Files\Java\jdk-21”
🌐
BeginnersBook
beginnersbook.com › 2024 › 05 › how-to-set-path-in-java
How to set Path in Java
May 26, 2024 - Open a new Command Prompt window (press Win + R, type cmd, and press Enter). Type the following command and press Enter: java -version · If the path is set correctly, you should see java version information.
🌐
Just Academy
justacademy.co › index.php › blog-detail › how-to-set-path-in-cmd-for-java
How to Set Path in CMD for Java
15) In the System Properties window, click on ‘Advanced system settings’, then click the ‘Environment Variables’ button. 16) In the Environment Variables window, under System variables, find the ‘Path’ variable and click ‘Edit’. 17) Click ‘New’ and paste the path to the JDK bin directory copied earlier. Click ‘OK’ to save the changes. 18) Close and reopen Command Prompt to apply the changes and now you can run Java commands from any directory.
Top answer
1 of 3
15

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 -version

jdk15.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 -version

jdk16.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 -version

Make sure you assign the appropriate JAVA_HOME value 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_HOME and the path to java must always refer to the exact same version of the JDK. If you mix them up, unpredictable things will happen!

Source Switch between different JDK versions in Windows | Oracle Pranav's Blog

2 of 3
0

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;...
Top answer
1 of 2
1
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
2 of 2
1
Use -D to set Java properties when running from the command line. For your case: -Djava.library.path=/path/to/libs In this above case, java.library.path is the property name, and /path/to/libs is the property value. Stolen from https://stackoverflow.com/questions/20038789/default-java-library-path
🌐
TechVidvan
techvidvan.com › tutorials › how-to-set-path-in-java
How to set path in Java? - TechVidvan
March 21, 2024 - Open Command Prompt: To open the Command Prompt, go to the search, type cmd, and press Enter. Copy JDK Path: Navigate to the bin directory of your JDK installation (e.g., C:\Program Files\Java\jdk_version\bin), and copy the complete path. Set PATH Temporarily: In the Command Prompt, type the ...
🌐
Edureka
edureka.co › blog › how-to-set-path-in-java
How To Set Java Path? Setting Java_Home Variable in Windows | Edureka
November 22, 2024 - Open System settings and go to advanced system settings. Click on the environment variables in the tab. Go to user variables and click on new. Add the variable name as ‘path’. Copy the path of the JDK bin directory.
🌐
Oracle
docs.oracle.com › javase › tutorial › essential › environment › paths.html
PATH and CLASSPATH (The Java™ Tutorials > Essential Java Classes > The Platform Environment)
If the version is old or you get the error java: Command not found, then the path is not properly set. To set the path permanently, set the path in your startup file.
🌐
PrepBytes
prepbytes.com › home › java › how to set path in java
How to set path in Java
July 26, 2023 - Within the system variables, click on the "New" button. In the "Edit System Variable" window, enter "JAVA_HOME" as the variable name and set its value to the path where the JDK folder is located. Afterward, click the "OK" button.