简体   繁体   English

在Linux中如何检查脚本安装了哪个Java版本?

[英]In Linux How to check through scripts what version of Java is installed?

alternate --config is updated 备用--config更新

java -version works java -version工作

How do i verify if java is installed if java is installed and what version. 如果安装了Java以及什么版本,我如何验证是否安装了Java。 What is the correct way. 什么是正确的方法。

I think this is what you want: 我认为这是您想要的:

#!/bin/bash
if [ -x /usr/bin/java ] ; then
        java -version 2>&1 | head -1 | awk -F '"' '{print $2}'
else
        exit
fi

OUTPUT (on my linux box): 输出 (在我的Linux机器上):

1.6.0_18

check whether the java executable is present. 检查是否存在Java可执行文件。 if yes, then print the version. 如果是,则打印版本。

I hope the following code may help you, please try it, 希望以下代码对您有所帮助,请尝试一下,

a=`(java -version) 2>&1`
if [[ "$a" == *1.7* ]]
then 
    echo '1.7'
fi 

Depends on how you expect it to be installed; 取决于您期望如何安装; there are two ways to find the java binary: 有两种找到java二进制文件的方法:

  • look for java in PATH (which is what you're doing by executing java -version ) 在PATH中查找java (这是通过执行java -version执行的操作)
  • if JAVA_HOME is defined, treat that as the preferred installation and run $JAVA_HOME/bin/java -version 如果定义了JAVA_HOME ,则将其视为首选安装,然后运行$JAVA_HOME/bin/java -version

After that, just extract the version number from the output as you need (see slayedbylucifer's answer above). 之后,只需根据需要从输出中提取版本号(请参见上面slayedbylucifer的回答)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM