[英]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
二进制文件的方法:
java
in PATH (which is what you're doing by executing java -version
) java
(这是通过执行java -version
执行的操作) 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.