Bash command to check if Oracle or OpenJDK java version is installed on Linux
65,032
Solution 1
if [[ $(java -version 2>&1) == *"OpenJDK"* ]]; then echo ok; else echo 'not ok'; fi
Solution 2
java -version 2>&1 | grep "OpenJDK Runtime" | wc -l
returns 0 if using Oracle JDK, 1 if using OpenJDK
Bash condition:
if [[ $(java -version 2>&1 | grep "OpenJDK Runtime") ]]
Solution 3
You can use below shell script for you work:
#!/bin/bash
declare -a JAVA=($(sudo find / -name java | grep -v grep | grep "/bin/java"))
for javapath in "${JAVA[@]}"
do
if [[ $(sudo ${javapath} -version 2>&1) != *"OpenJDK"* ]]; then
export JAVAVER=$(${javapath} -version 2>&1);
echo "Java Location -- ${javapath}"
echo "${JAVAVER}"
fi
done
NOTE: This will cover all java executable files running on you hosts.
Author by
Basil Musa
Lost in the code caribbeans, I try to find my way out, fortunate enough to stumble across stackoverflow.com, after which my life changed, to the better. Because of stackoverflow.com, I'm now living happily ever after.
Updated on July 10, 2022Comments
-
Basil Musa almost 2 years
I need a bash line to check if java version currently installed is Oracle's or OpenJDK.
A one-liner by parsing the output of the
java -version
command:java -version
java Oracle output:
java version "1.7.0_80" Java(TM) SE Runtime Environment (build 1.7.0_80-b15) Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)
java OpenJDK Output:
java version "1.7.0_91" OpenJDK Runtime Environment (amzn-2.6.2.2.63.amzn1-x86_64 u91-b00) OpenJDK 64-Bit Server VM (build 24.91-b01, mixed mode)
-
markusk over 5 yearsBash condition can be written as
if java -version 2>&1 | grep -q "OpenJDK Runtime"; then ...
. No need for[[ ... ]]
or$( ... )
.