2 minutes read

If you are one of those people who works on java and tries to jump between java versions for individual project needs, this post can help save some time.

First, let us look at how we can change the JAVA_HOME environment variable globally for a user on Windows 10.

Execute SystemPropertiesAdvanced.exe command from windows Run desktop app.

This will open the System Properties window. Click on the Environment Variables button located at the bottom of this window. Under System variables view click New button and enter Variable name as JAVA_HOME and Variable value as windows path to your java home location.

java home  Switch between Java versions on Windows OS java home

In order for windows to recognize all java executables, we need to provide java bin directory to PATH environment variable. PATH variable will also be located under System variables view. Click on Edit button and prepend %JAVA_HOME%\bin; and press ok to save PATH environment variable.

Verify Java version: Open a command prompt from Run desktop app and execute java -version. It will show the java version now.

java version  Switch between Java versions on Windows OS java version


Problem: All this is fine if it needs to be done once, but what if you are switching between projects and you need to change java home variable for each project requirement? It becomes a tedious job, so I wrote a small windows batch script which makes switching java versions convenient.

Solution: Create a “setjava.bat” file and save this file with below content and do not forget to change JAVA_PATH for your system. Also, remember to save this file with .bat extension.

echo off<br>
REM IMPORTANT<br>
REM RUN THIS SCRIPT AS ADMINISTRATOR<br>
set JAVA_REQ_VER=%1<br>
IF "%JAVA_REQ_VER%" == "7" (<br>
set JAVA_PATH="C:\Progra~1\Java\jdk1.7.0_79"<br>
) ELSE (<br>
REM At present I only need to assign 8 by default if not 7<br>
set JAVA_PATH="C:\Progra~1\Java\jdk1.8.0_111"<br>
)<br>
setx /M JAVA_HOME "%JAVA_PATH%"

Now you just run this script with administrator privileges.

Example: setjava 8, setjava 7

References:

Java

Technet.microsoft.com


Simon

I am a Fullstack developer and Consultant with an experience of 9+ years in the industry. I mainly work on Java, React, Javascript, NodeJs, Elasticsearch and Botpress.

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.