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 OverflowPerhaps 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
You can obtain java version via:
JAVA_VER=$(java -version 2>&1 | awk -F '"' '/version/ {print $2}' | awk -F '.' '{sub("^$", "0", $2); print
2}')
it will give you 16 for java like 1.6.0_13, 15 for version like 1.5.0_17, 110 for openjdk 11.0.6 2020-01-14 LTS and 210 for java version 21 2023-09-19 LTS.
So you can easily compare it in shell:
[ "$JAVA_VER" -ge 15 ] && echo "ok" || echo "too old"
UPDATE:
This code should work fine with openjdk and JAVA_TOOL_OPTIONS as mentioned in the comments.
Videos
This works for older "1.x.y" format as well as newer "x.y" format:
java -version 2>&1 \
| head -1 \
| cut -d'"' -f2 \
| sed 's/^1\.//' \
| cut -d'.' -f1
Source: https://odoepner.wordpress.com/2014/07/27/get-java-version-string-via-shell-commands/
One solution is to drop the date off the end by using cut. This approach would result in:
JAVA_MAJOR_VERSION=$($JAVA -version 2>&1 | sed -E -n 's/.* version "([^.-]*).*"/\1/p' | cut -d' ' -f1)
This approach splits on the space character (-d' ') and takes the first field. Based upon the input, that would drop the openjdk appended date.
I was editing the wrong file(/etc/profile) all that time. Adding following lines to /etc/bashrc resolved the issue.
export JAVA_HOME="/usr/java/jdk1.8.0_172-amd64/"
export PATH=$JAVA_HOME/bin:$PATH
Thanks
Update : Please refer the comment by @jsbillings below. creating a custom script in
/etc/profile.d/and adding above lines is the most recommended.
Each user has his own variable environment. Thus, it's normal that values of
PATHandJAVA_HOMEvariables are different from one user (root) to another one (non-root user).A persistent way to save environment variables :
Add your environment variable by editing
~/.bashrcfile :vim ~/.bashrcLoad the changes you did in
~/.bashrcusing :source ~/.bashrc
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 -versionjdk15.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 -versionjdk16.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 -versionMake sure you assign the appropriate
JAVA_HOMEvalue 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_HOMEand the path to java must always refer to the exact same version of theJDK. If you mix them up, unpredictable things will happen!
Source Switch between different JDK versions in Windows | Oracle Pranav's Blog
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;...