Bash命令检查Linux上是否安装了Oracle或OpenJDK java版本

时间:2022-10-02 07:00:05

I need a bash line to check if java version currently installed is Oracle's or OpenJDK.

我需要一个bash行来检查当前安装的java版本是Oracle还是OpenJDK。

A one-liner by parsing the output of the java -version command:

通过解析java -version命令的输出来实现单行:

java -version

java Oracle output:

java Oracle输出:

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 OpenJDK输出:

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)

2 个解决方案

#1


12  

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

#2


14  

java -version 2>&1 | grep "OpenJDK Runtime" | wc -l

returns 0 if using Oracle JDK, 1 if using OpenJDK

如果使用Oracle JDK,则返回0;如果使用OpenJDK,则返回1

Bash condition:

Bash条件:

if [[ $(java -version 2>&1 | grep "OpenJDK Runtime") ]]

#1


12  

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

#2


14  

java -version 2>&1 | grep "OpenJDK Runtime" | wc -l

returns 0 if using Oracle JDK, 1 if using OpenJDK

如果使用Oracle JDK,则返回0;如果使用OpenJDK,则返回1

Bash condition:

Bash条件:

if [[ $(java -version 2>&1 | grep "OpenJDK Runtime") ]]