Bash command to check if Oracle or OpenJDK java version is installed on Linux

JavaLinuxBash

Java Problem Overview


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)

Java Solutions


Solution 1 - Java

if [[ $(java -version 2>&1) == *"OpenJDK"* ]]; then echo ok; else echo 'not ok'; fi

Solution 2 - Java

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 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBasil MusaView Question on Stackoverflow
Solution 1 - JavaCortwaveView Answer on Stackoverflow
Solution 2 - JavajchampemontView Answer on Stackoverflow
Solution 3 - JavaSantosh GaroleView Answer on Stackoverflow