If you don't have admin rights, use the below command to set environment variables for java using the command prompt

setx JAVA_HOME "C:\Program Files\Java\jdk1.8.0"
setx PATH "%PATH%;%JAVA_HOME%\bin";

Modify the environment variable.

setx -m JAVA_HOME "C:\Program Files\Java\jdk1.8.0"
setx -m PATH "%PATH%;%JAVA_HOME%\bin";
Answer from Sathiamoorthy on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › set java_home variable in windows, mac os x, and linux
Set JAVA_HOME on Windows 7, 8, 10, Mac OS X, Linux | Baeldung
March 26, 2025 - Please note that /etc/environment isn’t a script, but a list of assignment expressions (that’s why export isn’t used). This file is read at the time of login. To set JAVA_HOME using /etc/profile, here’s what we’ll add to the file: ... In this article, we covered how to set the JAVA_HOME environment variable on Windows, Mac OS X, and Linux.
🌐
Wikihow
wikihow.com › computers and electronics › software › programming › java › how to set java_home on windows, linux, & mac
How to Set JAVA_HOME on Windows, Linux, & Mac
March 5, 2025 - If the file has existing contents, just scroll down past them to enter the following text on individual lines: export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-23.0.2.jdk/Contents/Home
🌐
Planetary Data System
pds.nasa.gov › datastandards › training › documents › Finding and Setting JAVA HOME.pdf pdf
Finding and Setting JAVA HOME
% JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.71.x86_64/jre · % export JAVA_HOME · C-shell-type shells can do this in a single step: % setenv JAVA_HOME /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.71.x86_64/jre · and there's an alternate syntax that works just as well: % set JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.71.x86_64/jre ·
🌐
TheServerSide
theserverside.com › feature › How-to-set-JAVA_HOME-in-Windows-and-echo-the-result
How to set JAVA_HOME in Windows | TheServerSide
If you choose to install Java manually or you want to update the JAVA_HOME environment variable on Windows, the easiest route is to use the Windows environment variables editor.
🌐
Atlassian
confluence.atlassian.com › spaces › DOC › pages › 8895 › Setting+the+JAVA_HOME+Variable+in+Windows
Setting the JAVA_HOME Variable in Windows | Confluence Data Center 10.2 | Atlassian Documentation
To install Confluence manually on Windows, you will need to set an environment variable to point Confluence to the your Java installation directory. This information is only relevant if you're installing Confluence manually on a Windows server. If you're using the installer, you don't need to do this. In most cases you should set the JRE_HOME environment variable, but if it is not set, Confluence will use JAVA_HOME.
🌐
Oracle
docs.oracle.com › en › cloud › saas › enterprise-performance-management-common › diepm › epm_set_java_home_104x6dd63633_106x6dd6441c.html
Setting JAVA_HOME
June 4, 2026 - setenv JAVA_HOME jdk-install-dir export JAVA_HOME PATH=$JAVA_HOME/bin:$PATH export PATH setenv PATH $JAVA_HOME/bin:$PATH export PATH=$JAVA_HOME/bin:$PATH
🌐
Mkyong
mkyong.com › home › java › how to set java_home on windows (step-by-step guide)
How to Set JAVA_HOME on Windows (Step-by-Step Guide) - Mkyong.com
July 29, 2025 - Learn how to set the JAVA_HOME environment variable on Windows 10 or 11 with easy-to-follow steps and screenshots.
Find elsewhere
🌐
Medium
medium.com › @zorozeri › setting-up-java-home-5abae0118bfe
Setting up JAVA_HOME. Guideline for Windows, MacOS, and… | by Ahmad Azeri Chandra Bhuana | Medium
November 13, 2024 - Now let’s setup the JAVA_HOME. But first, you need to know what kind of shell you used. If you never change anything related with your shell, it should be either bash or zsh. You can see the type from the terminal’s title, or to be sure, run command : ... If you use bash, you need to update file .bashrc. But to be sure, you can also update file .bash_profile and .profile as well. echo 'export "JAVA_HOME=$(/usr/libexec/java_home)"' >> ~/.bashrc echo 'export "JAVA_HOME=$(/usr/libexec/java_home)"' >> ~/.bash_profile echo 'export "JAVA_HOME=$(/usr/libexec/java_home)"' >> ~/.profile
🌐
Blogger
javarevisited.blogspot.com › 2012 › 02 › how-to-set-javahome-environment-in.html
How to set JAVA_HOME environment in Linux, Unix and Windows? Example
Let us know if you come across any other differences between them. ... Aryasindhu Sahu said... A small modification : To set/export PATH for java- export PATH= ${PATH}:${JAVA_HOME}/bin check the '$' before {JAVA_HOME}, i.e.
🌐
The Windows Club
thewindowsclub.com › the windows club › general › how to set java_home in windows 11/10
How to set JAVA_HOME in Windows 11/10
Click on Edit button to open Edit environment variable window. Click on New button and type %JAVA_HOME%\bin.
Published   July 13, 2024
🌐
Atlassian
confluence.atlassian.com › spaces › CROWD051 › pages › 1295680551 › Setting+JAVA_HOME
Setting JAVA_HOME | Atlassian Support | Atlassian Documentation
September 29, 2023 - Add export PATH=$JAVA_HOME/bin:$PATH ... window · vi /etc/profile (replace vi with your favorite text editor) Add export JAVA_HOME=/path/to/java/home/dir on its own line at the end of the file ·...
🌐
Atlassian
confluence.atlassian.com › spaces › CROWD › pages › 61604243 › Setting+JAVA_HOME
Setting JAVA_HOME | Crowd Data Center 7.2 | Atlassian Documentation
Add export PATH=$JAVA_HOME/bin:$PATH ... window · vi /etc/profile (replace vi with your favorite text editor) Add export JAVA_HOME=/path/to/java/home/dir on its own line at the end of the file ·...
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;...
🌐
Red Hat
access.redhat.com › solutions › 1445833
How to set the JAVA_HOME environment variable? - Red Hat Customer Portal
For the bash shell, this file is /home/username/.bashrc. At the bottom of the file, type the following line, replacing the hypothetical path with the actual path to use on the operating system: export JAVA_HOME="/path/to/java/home".
🌐
GeeksforGeeks
geeksforgeeks.org › java › setting-environment-java
Setting up Environment Variables For Java - Complete Guide to Set JAVA_HOME - GeeksforGeeks
May 9, 2025 - Now we need to set JAVA_HOME. To do this, ... The above changes are temporary. To make the environment variable changes permanent, you need to add the export commands to your shell’s configuration file.
🌐
Chef
discourse.chef.io › chef infra (archive)
How to set JAVA_HOME AND PATH in linux and windows while writing chef cookbook - Chef Infra (archive) - Chef Questions
July 3, 2019 - Cookbook for Linux: execute "export JAVA_HOME" do command "export JAVA_HOME=#{jdk_java_home}" end execute "export" do command "export PATH=#{jdk_path}" end Path for linux: JAVA_HOME = /home/oracleweblogic/wls/jdk1.8.0_211 PATH=$PATH:/home/o...
🌐
Opsgenie Docs
docs.opsgenie.com › docs › setting-java_home
Setting JAVA_HOME - Opsgenie Docs
Click the Windows icon located ... then type This PC, when it appears in the start menu Right-click on the This PC icon on your desktop, then select Properties ......