Perhaps something like:

if type -p java; then
    echo found java executable in PATH
    _java=java
elif [[ -n "$JAVA_HOME" ]] && [[ -x "$JAVA_HOME/bin/java" ]];  then
    echo found java executable in JAVA_HOME     
    _java="$JAVA_HOME/bin/java"
else
    echo "no java"
fi

if [[ "$_java" ]]; then
    version=_java" -version 2>&1 | awk -F '"' '/version/ {print $2}')
    echo version "$version"
    if [[ "$version" > "1.5" ]]; then
        echo version is more than 1.5
    else         
        echo version is less than 1.5
    fi
fi
Answer from glenn jackman on Stack Overflow
🌐
Medium
sboardwell.medium.com › determine-java-version-the-easy-way-11f7cc99b020
Determine Java version the easy way | by Steve Boardwell | Medium
November 20, 2020 - Here’s a 3-liner example using the java.specification.version property to look for versions 1.8, 11, or 15. Little gems to look out for in the script: the awesome awk command with its built-in regular expression filter (so you don’t need to ...| grep... | awk... ) the equally useful curly braces grouping feature from bash for grouping multiple commands · #!/usr/bin/env bashset -euo pipefail# Check existence of JAVA_HOME [ -n "${JAVA_HOME:-}" ] && { echo "JAVA_HOME check: ADDED"; export PATH="${JAVA_HOME}/bin:$PATH"; } || { echo "JAVA_HOME check: MISSING"; }# Check 'java' as a command command -v java &> /dev/null && { echo "Java command check PASSED"; } || { echo "java command not found.
🌐
Unix.com
unix.com › unix for beginners q & a › unix for dummies questions & answers
echo Java version - UNIX for Dummies Questions & Answers - Unix Linux Community
April 22, 2008 - Hi When I say java -version, the following text is printed java version “1.4.2” Java™ 2 Runtime Environment, Standard Edition (build 1.4.2) Classic VM (build 1.4.2, J2RE 1.4.2 IBM AIX build ca142-20060421 (SR5) (JIT …
🌐
Linux.org
linux.org › home › forums › general linux forums › general linux topics
Check Java version from Command Line | Linux.org
May 28, 2020 - you can do a few things: bash-5.0$ java -version java version "1.8.0_231" Java(TM) SE Runtime Environment (build 1.8.0_231-b11) Java HotSpot(TM) 64-Bit Server VM (build 25.231-b11, mixed mode) bash-5.0$ bash-5.0$ bash-5.0$ which java /usr/lib64/java/bin/java bash-5.0$ echo $JAVA_HOME ...
🌐
Eed3si9n
eed3si9n.com › detecting-java-version-bash
detecting Java version from Bash · eed3si9n
# 8 for 1.8.0_nn, 9 for 9-ea etc, and "no_java" for undetected jdk_version() { local result local java_cmd if [[ -n $(type -p java) ]] then java_cmd=java elif [[ (-n "$JAVA_HOME") && (-x "$JAVA_HOME/bin/java") ]] then java_cmd="$JAVA_HOME/bin/java" fi local IFS=$'\n' # remove \r for Cygwin local lines=$("$java_cmd" -Xms32M -Xmx32M -version 2>&1 | tr '\r' '\n') if [[ -z $java_cmd ]] then result=no_java else for line in $lines; do if [[ (-z $result) && ($line = *"version \""*) ]] then local ver=$(echo $line | sed -e 's/.*version "\(.*\)"\(.*\)/\1/; 1q') # on macOS, sed doesn't support '?' if [[ $ver = "1."* ]] then result=$(echo $ver | sed -e 's/1\.\([0-9]*\)\(.*\)/\1/; 1q') else result=$(echo $ver | sed -e 's/\([0-9]*\)\(.*\)/\1/; 1q') fi fi done fi echo "$result" } v="$(jdk_version)" echo $v
🌐
HappyCoders.eu
happycoders.eu › java › how-to-switch-multiple-java-versions-windows
How to Change Java Versions in Windows (up to Java 25)
June 11, 2025 - To change the Java version on the command line, I have prepared some batch files that you can copy to your system. Here is the link: scripts-up-to-java25.zip ... I suggest you unpack the scripts to C:\Program Files\Java\scripts. ... @echo off if %1 == "Java 1.2" set JAVA_HOME=C:\Program Files (x86)\Java\jdk1.2.2 if %1 == "Java 1.3" set JAVA_HOME=C:\Program Files (x86)\Java\jdk1.3.1_28 ...
Find elsewhere
🌐
Oracle
java.com › en › download › help › version_manual.html
How to find Java version in Windows or Mac - Manual method
Learn how to find which Java version(s) are installed without running an applet on Windows or Mac
🌐
Mkyong
mkyong.com › home › java › how to set $java_home environment variable on macos
How to Set $JAVA_HOME environment variable on macOS - Mkyong.com
January 19, 2021 - For older Mac OS X, the bash is the default Terminal shell, and we can set the $JAVA_HOME environment variable in either ~/.bash_profile or ~/.bashrc. ... % source ~/.bash_profile % echo $JAVA_HOME /Library/Java/JavaVirtualMachines/jdk-16.jdk/Contents/Home
🌐
Real's HowTo
rgagnon.com › javadetails › java-0642.html
Get the current Java version from a BAT file - Real's Java How-to
@echo off cls setlocal ENABLEEXTENSIONS set KEY_NAME="HKLM\SOFTWARE\JavaSoft\Java Runtime Environment" set VALUE_NAME=CurrentVersion :: :: get the current version :: FOR /F "usebackq skip=4 tokens=3" %%A IN (`REG QUERY %KEY_NAME% /v %VALUE_NAME% 2^>nul`) DO ( set ValueValue=%%A ) if defined ValueValue ( @echo the current Java runtime is %ValueValue% ) else ( @echo %KEY_NAME%\%VALUE_NAME% not found.
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;...
🌐
Linuxize
linuxize.com › home › java › how to check java version
How to Check Java Version | Linuxize
March 29, 2026 - Many Java applications and build tools such as Maven, Gradle, and Tomcat rely on the JAVA_HOME environment variable to locate the Java installation. To check whether it is set, run: Terminal · echo $JAVA_HOME ·
🌐
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 - C:\Users\mkyon>java -version openjdk version "21.0.8" 2025-07-15 LTS OpenJDK Runtime Environment Temurin-21.0.8+9 (build 21.0.8+9-LTS) OpenJDK 64-Bit Server VM Temurin-21.0.8+9 (build 21.0.8+9-LTS, mixed mode, sharing) C:\Users\mkyon>javac -version javac 21.0.8 C:\> echo %JAVA_HOME% C:\Program Files\Java\jdk-21
🌐
Atlassian
jira.atlassian.com › browse › JRA-59656
config.bat script does not correctly detect Java version ... - Jira
@ECHO OFF setlocal rem rem check for correct java version by parsing out put of java -version rem we expect first line to be in format "java version 1.8.0" and assert that minor version number will be 8 or higher rem set EXPECTED_JAVA_VERSION=8 for /f "tokens=3" %%g in ('java -version 2^>^&1 ^| findstr /i "version"') do ( set JAVAVER=%%g ) set JAVAVER=%JAVAVER:"=% for /f "delims=. tokens=2" %%v in ("%JAVAVER%") do ( set JAVA=%%v goto loaded_version ) :loaded_version IF %JAVA% LSS %EXPECTED_JAVA_VERSION% goto wrong_version goto:eof :wrong_version echo ******************************************************************************* echo ******* Wrong JVM version!