The JDK includes the JRE which you can launch by using the java executable in the bin folder. You use this executable just like any other executable.
When you type java in the command line it is actually shorthand. It searches your PATH until it finds the first matching java executable. If you want to specify a different java executable you can give the absolute path to the executable.
C:\Users\Avril> "C:\Program Files\Java\jdk-11.0.1\bin\java" -jar path\to\file.jar
You may be wondering, if you've set JAVA_HOME and PATH to point to JDK-11, why does java -version still use Java 8?
Take a look at your PATH and you'll likely find something like C:\ProgramData\Oracle\Java\javapath as one of the first entries (see this). This entry was added automatically when you installed Java 8 and points to the Java 8 executables (java, javaw, and javaws). Since it's before your %JAVA_HOME%\bin entry, it takes precedence. However, ...\javapath doesn't contain javac and that's why javac -version used JAVA_HOME (Java 11).
The JDK includes the JRE which you can launch by using the java executable in the bin folder. You use this executable just like any other executable.
When you type java in the command line it is actually shorthand. It searches your PATH until it finds the first matching java executable. If you want to specify a different java executable you can give the absolute path to the executable.
C:\Users\Avril> "C:\Program Files\Java\jdk-11.0.1\bin\java" -jar path\to\file.jar
You may be wondering, if you've set JAVA_HOME and PATH to point to JDK-11, why does java -version still use Java 8?
Take a look at your PATH and you'll likely find something like C:\ProgramData\Oracle\Java\javapath as one of the first entries (see this). This entry was added automatically when you installed Java 8 and points to the Java 8 executables (java, javaw, and javaws). Since it's before your %JAVA_HOME%\bin entry, it takes precedence. However, ...\javapath doesn't contain javac and that's why javac -version used JAVA_HOME (Java 11).
bin folder contains all the traditional JRE tools. In Java 11, both JDK and JRE tools are consolidated so that there is no JRE within JDK 11.
The set command only works for the current terminal. To permanently set a system or user environment variable you can use setx.
You can set the variable for the current user like this:
setx JAVA_HOME "C:\Program Files\Java\jdk1.7.0_72"
You can also set the variable system wide (Note: The terminal must be run as administrator fo this) by running the same command with the /m option:
setx JAVA_HOME "C:\Program Files\Java\jdk1.7.0_72" /m
The variable will be available in all new terminal session, but not the current one. If you also want to use the path in the same session, you need to use both set and setx.
You can avoid manipulating the PATH variable if you just once put %JAVA_HOME%\bin in there, instead of the full JDK path. If you change JAVA_HOME, PATH will be point to the new location too.
There are also a few environment variable editors as alternative to the cumbersome Windows environment variable settings. See "Is there a convenient way to edit PATH in Windows 7?" on Super User.
In case if someone want to switch frequently in each new command window then I am using following approach.
Command Prompt Version:
Create batch file using below code. You can add n number of version using if and else blocks.
@echo off
if "%~1" == "11" (
set "JAVA_HOME=C:\Software\openjdk-11+28_windows-x64_bin\jdk-11"
) else (
set "JAVA_HOME=C:\Program Files\Java\jdk1.8.0_151"
)
set "Path=%JAVA_HOME%\bin;%Path%"
java -version
Save this batch file as SJV.bat and add this file location in your machine's Path environment variable. So now SJV will act as command to "Switch Java Version".
Now open new command window and just type SJV 11 it will switch to Java 11.
Type SJV 8 it will switch to Java 8.
PowerShell Version
Create powershell(ps1) file using below code. You can add n number of version using if and else blocks.
If($args[0] -eq "11")
{
$env:JAVA_HOME = 'C:\Software\openjdk-11+28_windows-x64_bin\jdk-11'
}else{
$env:JAVA_HOME = 'C:\Program Files\Java\jdk1.8.0_151'
}
$env:Path = $env:JAVA_HOME+'\bin;'+$env:Path
java -version
Save this script file as SJV.ps1 and add this file location in your machine's Path environment variable. So now SJV will act as command to "Switch Java Version".
Now open new powershell window and just type SJV 11 it will switch to Java 11.
Type SJV 8 OR SJV it will switch to Java 8.
I hope this help someone who want to change it frequently.
How can I switch to Java 8 to Java 21?
How do I switch between Java versions?
java - Switch JDK version in Windows 10 cmd - Stack Overflow
When will Java 11 replace Java 8 as the default java?
Videos
A couple things to note is that for the Environment Variables, I cannot access the System Part, only the user variables (despite no one else having a user on the PC)
I use windows 11, not sure if there's a difference between 10 and 11 for that
Change your PATH variable so that it has the location of the jdk5/bin directory:
- Start -> Control Panel -> System -> Advanced
- Click on Environment Variables, under System Variables, find PATH, and click on it.
- In the Edit windows, modify PATH by adding the location of your jdk5/bin directory to the beginning. If you do not have the item PATH, you may select to add a new variable and add PATH as the name and the location of the directory as the value.
- Close the window.
- Reopen Command prompt window, and run
java -version
In the command shell:
set JAVA_HOME=C:\jdk1.6.0u24
set PATH=%JAVA_HOME%\bin;%PATH%
That will temporarily set up the environment in the command shell. Maven, Ant, etc. will pick up on your new version of Java without having to go to the Control Panel repeatedly.
Tools like Eclipse should be able to select which JDK to use in their own configuration tools for use within their environments.
So, I have two different versions of Java in my system, 8 and 16. Depending on the project, I need to switch between the two. Right now, I need to use 16, but when I run java -version I see, I'm still using "1.8.0_202".
I have changed my JAVA_HOME variable to point to the location of Java 16. Do I need to uninstall Java 8 in order to use Java 16? I'd rather not do that.
Here's my guide for Windows 10.
Step 1. Go to System Properties. Click on Environment Variables
Step 2. Add new variables, such as JAVA_8_HOME
JAVA_8_HOME:%ProgramFiles%\Java\jdk1.8.0_152\binJAVA_9_HOME:%ProgramFiles%\Java\jdk-9.0.1\binJAVA_HOME:%JAVA_8_HOME%
In my case, JAVA_8_HOME(JDK8) is pointing to C:\Program Files\Java\jdk1.8.0_152\bin. You can replace this with your own path to javac. %JAVA_HOME% has a default value pointing to JAVA_8_HOME, which is the path for JDK8. That's my preference, feel free to adjust accordingly.
Step 3. Select PATH and click on Edit. PATH
Step 4. Click on New and add %JAVA_HOME%. %JAVA_HOME% will be added to PATH automatically every time you launch a command prompt.
In order to switch JDK version in cmd, here's the trick. Step 5. I created a batch file with
@echo off
:: Switch JDK version
DOSKEY java8=SET PATH=%JAVA_8_HOME%;%PATH%;
DOSKEY java9=SET PATH=%JAVA_9_HOME%;%PATH%
Basically, it disables echo and creates two alias. In batch file any string after :: is the comments. Every time, java8 or java9 is called, it re-exports %PATH% with the new JDK path. Save it as profile.bat. You can name it whatever you want.
Step 6.
Search for regedit (Registry Editor). Click on Edit > New > String Value. Give AutoRun as the Value name and %USERPROFILE%\profile.bat as the Value data. Here, please put your actual path value to the profile.bat we just created. So, whenever a command prompt is opened, it automatically loads profile.bat, which creates those two alias in the script.
Step 7. Close any command prompt you're using or just open a new command prompt. This is because your changes will not affect opened cmd window. Environment changes only happens to new CMD.
Step 8. Verify your results here.
If you're using different Python versions, same trick applies, too. Find my python environment settings here.
I created scripts for every version of Java that I have installed on my PC, and use those whenever I need to change the Java version through the command line. Here is an example of one, should be self-explanatory really:
@echo off
echo Setting JAVA_HOME
set JAVA_HOME=C:\Program Files\Java\jdk-10
echo Setting PATH
set PATH=C:\Program Files\Java\jdk-10\bin;%PATH%
echo Display java version
java -version
java -version is running the wrong version of java.
Diagnostics:
>java -version
java version "1.8.0_66"
Java(TM) SE Runtime Environment (build 1.8.0_66-b18)
Java HotSpot(TM) 64-Bit Server VM (build 25.66-b18, mixed mode)
the following is the Java related contents from the output of
PATH:
PATH=C:\ProgramData\Oracle\Java\javapath; ... C:\Program Files\Java\jdk1.6.0_45\bin
Conclusion:
From the above output we can deduce that C:\ProgramData\Oracle\Java\javapath is 1.8.0_66.
You need to change your PATH to put C:\Program Files\Java\jdk1.6.0_45\bin first.
I noticed that after checking the path per your suggestion. Windows 10 does not allow me to edit the path because it says "This environment variable is too large." I know there should be another question to deal with this separately.
You also need to clean up your path. My guess is you have a lot of duplicate entries.
I have the same problem, I have set JAVA_HOME:
C:\Program Files\Java\jdk1.7.0_75
and Path to:
%JAVA_HOME%\bin
I need run jdk 7. When I run java -version it always appear jdk 8.
I solved it with: in System Environment --> Path --> order %JAVA_HOME%\bin to first.