从 BASH 脚本检查 Java 版本的正确方法

发布于 2024-12-03 06:40:18 字数 66 浏览 3 评论 0原文

如何从 bash 脚本检查 Java 是否可用(在 PATH 中或通过 JAVA_HOME)并确保版本至少为 1.5?

How can I check whether Java is available (in the PATH or via JAVA_HOME) from a bash script and make sure the version is at least 1.5?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(20

七色彩虹 2024-12-10 06:40:18

也许是这样的:

if type -p java; then
    echo found java executable in PATH
    _java=java
elif [[ -n "$JAVA_HOME" ]] && [[ -x "$JAVA_HOME/bin/java" ]];  then
    echo found java executable in JAVA_HOME     
    _java="$JAVA_HOME/bin/java"
else
    echo "no java"
fi

if [[ "$_java" ]]; then
    version=$("$_java" -version 2>&1 | awk -F '"' '/version/ {print $2}')
    echo version "$version"
    if [[ "$version" > "1.5" ]]; then
        echo version is more than 1.5
    else         
        echo version is less than 1.5
    fi
fi

Perhaps something like:

if type -p java; then
    echo found java executable in PATH
    _java=java
elif [[ -n "$JAVA_HOME" ]] && [[ -x "$JAVA_HOME/bin/java" ]];  then
    echo found java executable in JAVA_HOME     
    _java="$JAVA_HOME/bin/java"
else
    echo "no java"
fi

if [[ "$_java" ]]; then
    version=$("$_java" -version 2>&1 | awk -F '"' '/version/ {print $2}')
    echo version "$version"
    if [[ "$version" > "1.5" ]]; then
        echo version is more than 1.5
    else         
        echo version is less than 1.5
    fi
fi
吻泪 2024-12-10 06:40:18

您可以通过以下方式获取java版本:

JAVA_VER=$(java -version 2>&1 | awk -F '"' '/version/ {print $2}' | awk -F '.' '{sub("^$", "0", $2); print $1$2}')

对于java,它会给您16,如1.6.0_13,对于15版本,如1.5.0_17110 用于 openjdk 11.0.6 2020-01-14 LTS210 用于java 版本 21 2023-09-19 LTS

所以你可以轻松地在 shell 中比较它:

[ "$JAVA_VER" -ge 15 ] && echo "ok" || echo "too old"

更新:
如评论中所述,此代码应该可以与 openjdk 和 JAVA_TOOL_OPTIONS 正常工作。

You can obtain java version via:

JAVA_VER=$(java -version 2>&1 | awk -F '"' '/version/ {print $2}' | awk -F '.' '{sub("^
quot;, "0", $2); print $1$2}')

it will give you 16 for java like 1.6.0_13, 15 for version like 1.5.0_17, 110 for openjdk 11.0.6 2020-01-14 LTS and 210 for java version 21 2023-09-19 LTS.

So you can easily compare it in shell:

[ "$JAVA_VER" -ge 15 ] && echo "ok" || echo "too old"

UPDATE:
This code should work fine with openjdk and JAVA_TOOL_OPTIONS as mentioned in the comments.

当梦初醒 2024-12-10 06:40:18

上面的答案仅适用于特定的 Java 版本(通常适用于 Java 9 之前的版本或 Java 8 之后的版本)。

我编写了一个简单的单行代码,它将返回 Java 版本 6 到 11 的整数(可能还有所有未来版本,直到他们再次更改它!)。

它基本上会删除版本号开头的“1.”(如果存在),然后仅考虑下一个“.”之前的第一个数字

。 &1 |头 -1 | 剪切 -d'"' -f2 | sed '/^1\./s///' | 剪切 -d'.'

The answers above work correctly only for specific Java versions (usually for the ones before Java 9 or for the ones after Java 8.

I wrote a simple one liner that will return an integer for Java versions 6 through 11 (and possibly all future versions, until they change it again!).

It basically drops the "1." at the beginning of the version number, if it exists, and then considers only the first number before the next ".".

java -version 2>&1 | head -1 | cut -d'"' -f2 | sed '/^1\./s///' | cut -d'.' -f1

很糊涂小朋友 2024-12-10 06:40:18

您可以发出 java -version 并读取 &解析输出

java -version 2>&1 >/dev/null | grep 'java version' | awk '{print $3}'

You can issue java -version and read & parse the output

java -version 2>&1 >/dev/null | grep 'java version' | awk '{print $3}'
叹沉浮 2024-12-10 06:40:18

您还可以将jdk类版本号与javap进行比较:

javap -verbose java.lang.String | grep "major version" | cut -d " " -f5

如果是java 8,则会打印52,而对于java 11,则会打印55。有了这个,您可以进行简单的比较,而不必处理版本字符串解析。

You could also compare the jdk class version number with javap:

javap -verbose java.lang.String | grep "major version" | cut -d " " -f5

If its java 8, this will print 52, and 55 for java 11. With this you can make a simple comparison and you don't have to deal with the version string parsing.

一袭白衣梦中忆 2024-12-10 06:40:18

我编写了一个适用于 JDK 9 和 JDK 10 的 bash 函数。

#!/bin/bash

# returns the JDK version.
# 8 for 1.8.0_nn, 9 for 9-ea etc, and "no_java" for undetected
jdk_version() {
  local result
  local java_cmd
  if [[ -n $(type -p java) ]]
  then
    java_cmd=java
  elif [[ (-n "$JAVA_HOME") && (-x "$JAVA_HOME/bin/java") ]]
  then
    java_cmd="$JAVA_HOME/bin/java"
  fi
  local IFS=

对于 Java 8("1.8.0_151" 等),这将返回 8,而 9< /code> for Java 9("9-Debian" 等),这应该更容易进行进一步的比较。

\n' # remove \r for Cygwin local lines=$("$java_cmd" -Xms32M -Xmx32M -version 2>&1 | tr '\r' '\n') if [[ -z $java_cmd ]] then result=no_java else for line in $lines; do if [[ (-z $result) && ($line = *"version \""*) ]] then local ver=$(echo $line | sed -e 's/.*version "\(.*\)"\(.*\)/\1/; 1q') # on macOS, sed doesn't support '?' if [[ $ver = "1."* ]] then result=$(echo $ver | sed -e 's/1\.\([0-9]*\)\(.*\)/\1/; 1q') else result=$(echo $ver | sed -e 's/\([0-9]*\)\(.*\)/\1/; 1q') fi fi done fi echo "$result" } v="$(jdk_version)" echo $v

对于 Java 8("1.8.0_151" 等),这将返回 8,而 9< /code> for Java 9("9-Debian" 等),这应该更容易进行进一步的比较。

I wrote a bash function that should work for JDK 9 and JDK 10.

#!/bin/bash

# returns the JDK version.
# 8 for 1.8.0_nn, 9 for 9-ea etc, and "no_java" for undetected
jdk_version() {
  local result
  local java_cmd
  if [[ -n $(type -p java) ]]
  then
    java_cmd=java
  elif [[ (-n "$JAVA_HOME") && (-x "$JAVA_HOME/bin/java") ]]
  then
    java_cmd="$JAVA_HOME/bin/java"
  fi
  local IFS=

This returns 8 for Java 8 ("1.8.0_151" etc), and 9 for Java 9 ("9-Debian" etc), which should make it easier to do the further comparison.

\n' # remove \r for Cygwin local lines=$("$java_cmd" -Xms32M -Xmx32M -version 2>&1 | tr '\r' '\n') if [[ -z $java_cmd ]] then result=no_java else for line in $lines; do if [[ (-z $result) && ($line = *"version \""*) ]] then local ver=$(echo $line | sed -e 's/.*version "\(.*\)"\(.*\)/\1/; 1q') # on macOS, sed doesn't support '?' if [[ $ver = "1."* ]] then result=$(echo $ver | sed -e 's/1\.\([0-9]*\)\(.*\)/\1/; 1q') else result=$(echo $ver | sed -e 's/\([0-9]*\)\(.*\)/\1/; 1q') fi fi done fi echo "$result" } v="$(jdk_version)" echo $v

This returns 8 for Java 8 ("1.8.0_151" etc), and 9 for Java 9 ("9-Debian" etc), which should make it easier to do the further comparison.

夜声 2024-12-10 06:40:18

仅使用带有 Perl 样式正则表达式的 grep 来确定 Java 版本可以如下完成:

java -version 2>&1 | grep -oP 'version "?(1\.)?\K\d+'

这将打印 Java 9 或更高版本的主要版本以及低于 9 的版本的次要版本,例如 8 for Java 1.8 和 Java 11.0.411

它可以像这样使用:

JAVA_MAJOR_VERSION=$(java -version 2>&1 | grep -oP 'version "?(1\.)?\K\d+' || true)
if [[ $JAVA_MAJOR_VERSION -lt 8 ]]; then
  echo "Java 8 or higher is required!"
  exit 1
fi

||添加 true 是为了防止在未安装 Java 且启用了 Shell 选项 set -e 的情况下脚本中止。

Determining the Java version only using grep with Perl-style regex can be done like this:

java -version 2>&1 | grep -oP 'version "?(1\.)?\K\d+'

This will print the major version for Java 9 or higher and the minor version for versions lower than 9, for example 8 for Java 1.8 and 11 for Java 11.0.4.

It can be used like this:

JAVA_MAJOR_VERSION=$(java -version 2>&1 | grep -oP 'version "?(1\.)?\K\d+' || true)
if [[ $JAVA_MAJOR_VERSION -lt 8 ]]; then
  echo "Java 8 or higher is required!"
  exit 1
fi

The || true was added to prevent the script from aborting in case Java is not installed and the Shell option set -e was enabled.

﹉夏雨初晴づ 2024-12-10 06:40:18

不同答案的组合:

JAVA_VER=$(java -version 2>&1 | grep -i version | sed 's/.*version ".*\.\(.*\)\..*"/\1/; 1q')
  • 对于 Java 7 返回 7,对于 Java 8
  • 返回 8 适用于 OpenJDK 和 Oracle JDK
  • 即使设置了 JAVA_TOOL_OPTIONS,也可以使用

A combination of different answers:

JAVA_VER=$(java -version 2>&1 | grep -i version | sed 's/.*version ".*\.\(.*\)\..*"/\1/; 1q')
  • Returns 7 for Java 7 and 8 for Java 8
  • Works with OpenJDK and with Oracle JDK
  • Works even if the JAVA_TOOL_OPTIONS is set
森林迷了鹿 2024-12-10 06:40:18
  1. TERMINAL 中
java -version |& grep 'version' |& awk -F\" '{ split($2,a,"."); print a[1]"."a[2]}'
  1. 在 BASH 中的
#!/bin/bash


echo `java -version 2>&1 | grep 'version' 2>&1 | awk -F\" '{ split($2,a,"."); print a[1]"."a[2]}'`
jver=`java -version 2>&1 | grep 'version' 2>&1 | awk -F\" '{ split($2,a,"."); print a[1]"."a[2]}'`

if [[ $jver == "1.8" ]]; then                
     echo $jver is java 8
else
     echo $jver is java 11
fi

适用于 SUN 和 Linux

  1. in TERMINAL
java -version |& grep 'version' |& awk -F\" '{ split($2,a,"."); print a[1]"."a[2]}'
  1. in BASH
#!/bin/bash


echo `java -version 2>&1 | grep 'version' 2>&1 | awk -F\" '{ split($2,a,"."); print a[1]"."a[2]}'`
jver=`java -version 2>&1 | grep 'version' 2>&1 | awk -F\" '{ split($2,a,"."); print a[1]"."a[2]}'`

if [[ $jver == "1.8" ]]; then                
     echo $jver is java 8
else
     echo $jver is java 11
fi

Works in SUN and Linux

ぃ双果 2024-12-10 06:40:18

我最终使用的方法是:

# Work out the JAVA version we are working with:
JAVA_VER_MAJOR=""
JAVA_VER_MINOR=""
JAVA_VER_BUILD=""

# Based on: http://stackoverflow.com/a/32026447
for token in $(java -version 2>&1 | grep -i version)
do
    if [[ $token =~ \"([[:digit:]])\.([[:digit:]])\.(.*)\" ]]
    then
        JAVA_VER_MAJOR=${BASH_REMATCH[1]}
        JAVA_VER_MINOR=${BASH_REMATCH[2]}
        JAVA_VER_BUILD=${BASH_REMATCH[3]}
        break
    fi
done

即使由于 grep 完成的过滤而将 JAVA_TOOL_OPTIONS 设置为某些内容,它也会正常工作。

The method I ended up using is:

# Work out the JAVA version we are working with:
JAVA_VER_MAJOR=""
JAVA_VER_MINOR=""
JAVA_VER_BUILD=""

# Based on: http://stackoverflow.com/a/32026447
for token in $(java -version 2>&1 | grep -i version)
do
    if [[ $token =~ \"([[:digit:]])\.([[:digit:]])\.(.*)\" ]]
    then
        JAVA_VER_MAJOR=${BASH_REMATCH[1]}
        JAVA_VER_MINOR=${BASH_REMATCH[2]}
        JAVA_VER_BUILD=${BASH_REMATCH[3]}
        break
    fi
done

It will work correctly even if JAVA_TOOL_OPTIONS is set to something due to filtering done by grep.

追星践月 2024-12-10 06:40:18

另一种方法是:
名为 release 的文件位于 $JAVA_HOME 中。
在 Java 15 (Debian) 上,它的内容为:

IMPLEMENTOR="Debian"
JAVA_VERSION="15.0.1"
JAVA_VERSION_DATE="2020-10-20"
MODULES="java.base java.compiler java.datatransfer java.xml java.prefs java.desktop java.instrument java.logging java.management java.security.sasl java.naming java.rmi java.management.rmi java.net.http java.scripting java.security.jgss java.transaction.xa java.sql java.sql.rowset java.xml.crypto java.se java.smartcardio jdk.accessibility jdk.internal.vm.ci jdk.management jdk.unsupported jdk.internal.vm.compiler jdk.aot jdk.internal.jvmstat jdk.attach jdk.charsets jdk.compiler jdk.crypto.ec jdk.crypto.cryptoki jdk.dynalink jdk.internal.ed jdk.editpad jdk.hotspot.agent jdk.httpserver jdk.incubator.foreign jdk.internal.opt jdk.jdeps jdk.jlink jdk.incubator.jpackage jdk.internal.le jdk.internal.vm.compiler.management jdk.jartool jdk.javadoc jdk.jcmd jdk.management.agent jdk.jconsole jdk.jdwp.agent jdk.jdi jdk.jfr jdk.jshell jdk.jsobject jdk.jstatd jdk.localedata jdk.management.jfr jdk.naming.dns jdk.naming.rmi jdk.net jdk.nio.mapmode jdk.sctp jdk.security.auth jdk.security.jgss jdk.unsupported.desktop jdk.xml.dom jdk.zipfs"
OS_ARCH="x86_64"
OS_NAME="Linux"
SOURCE=""

因此您会看到 JAVA_VERSION,其中包含版本号。

例如,

major=$(echo $JAVA_VERSION | cut -d. -f1)

major 设置为值 15(=主版本号),您可以进一步使用该值。

这是一个非常简单的解决方案。

Another way is:
A file called release is located in $JAVA_HOME.
On Java 15 (Debian) it has as content:

IMPLEMENTOR="Debian"
JAVA_VERSION="15.0.1"
JAVA_VERSION_DATE="2020-10-20"
MODULES="java.base java.compiler java.datatransfer java.xml java.prefs java.desktop java.instrument java.logging java.management java.security.sasl java.naming java.rmi java.management.rmi java.net.http java.scripting java.security.jgss java.transaction.xa java.sql java.sql.rowset java.xml.crypto java.se java.smartcardio jdk.accessibility jdk.internal.vm.ci jdk.management jdk.unsupported jdk.internal.vm.compiler jdk.aot jdk.internal.jvmstat jdk.attach jdk.charsets jdk.compiler jdk.crypto.ec jdk.crypto.cryptoki jdk.dynalink jdk.internal.ed jdk.editpad jdk.hotspot.agent jdk.httpserver jdk.incubator.foreign jdk.internal.opt jdk.jdeps jdk.jlink jdk.incubator.jpackage jdk.internal.le jdk.internal.vm.compiler.management jdk.jartool jdk.javadoc jdk.jcmd jdk.management.agent jdk.jconsole jdk.jdwp.agent jdk.jdi jdk.jfr jdk.jshell jdk.jsobject jdk.jstatd jdk.localedata jdk.management.jfr jdk.naming.dns jdk.naming.rmi jdk.net jdk.nio.mapmode jdk.sctp jdk.security.auth jdk.security.jgss jdk.unsupported.desktop jdk.xml.dom jdk.zipfs"
OS_ARCH="x86_64"
OS_NAME="Linux"
SOURCE=""

So you see JAVA_VERSION, which contains the version number.

For example,

major=$(echo $JAVA_VERSION | cut -d. -f1)

sets major to the value 15 (=Major version number), which you can use further.

It's a really simple solution.

深海蓝天 2024-12-10 06:40:18

使用 Bash 的内置解析功能获取 java 版本字符串:

#!/usr/bin/env bash

java_cmd=${1:-java}

# Captures version string between double-quotes
IFS='"' read -r _ java_version_string _ < <("${java_cmd}" -version 2>&1)

# Splits version string at dots and (underscore for patch number)
IFS='._' read -r \
  java_version_major \
  java_version_minor \
  java_version_build \
  java_version_patch \
  <<<"$java_version_string"

# Debug demo output
declare -p java_version_major java_version_minor java_version_build \
  java_version_patch

示例

系统默认 Java 17:

declare -- java_version_major="17"
declare -- java_version_minor="0"
declare -- java_version_build="3"
declare -- java_version_patch=""

作为参数提供的特定 Java 8 路径:

declare -- java_version_major="1"
declare -- java_version_minor="8"
declare -- java_version_build="0"
declare -- java_version_patch="352"

Getting java version string, using Bash's built-in parsing capabilities:

#!/usr/bin/env bash

java_cmd=${1:-java}

# Captures version string between double-quotes
IFS='"' read -r _ java_version_string _ < <("${java_cmd}" -version 2>&1)

# Splits version string at dots and (underscore for patch number)
IFS='._' read -r \
  java_version_major \
  java_version_minor \
  java_version_build \
  java_version_patch \
  <<<"$java_version_string"

# Debug demo output
declare -p java_version_major java_version_minor java_version_build \
  java_version_patch

Examples

System default Java 17:

declare -- java_version_major="17"
declare -- java_version_minor="0"
declare -- java_version_build="3"
declare -- java_version_patch=""

Specific Java 8 path provided as argument:

declare -- java_version_major="1"
declare -- java_version_minor="8"
declare -- java_version_build="0"
declare -- java_version_patch="352"
空‖城人不在 2024-12-10 06:40:18

我有一个类似的问题,我想检查安装了哪个版本的java来执行两种类型的应用程序启动。
下面的代码解决了我的问题

jdk_version() {
  local result
  local java_cmd
  if [[ -n $(type -p java) ]]
  then
    java_cmd=java
  elif [[ (-n "$JAVA_HOME") && (-x "$JAVA_HOME/bin/java") ]]
  then
    java_cmd="$JAVA_HOME/bin/java"
  fi
  local IFS=

来源

我希望能被证明是有帮助的< /em>

\n' # remove \r for Cygwin local lines=$("$java_cmd" -Xms32M -Xmx32M -version 2>&1 | tr '\r' '\n') if [[ -z $java_cmd ]] then result=no_java else for line in $lines; do if [[ (-z $result) && ($line = *"version \""*) ]] then local ver=$(echo $line | sed -e 's/.*version "\(.*\)"\(.*\)/\1/; 1q') # on macOS, sed doesn't support '?' if [[ $ver = "1."* ]] then result=$(echo $ver | sed -e 's/1\.\([0-9]*\)\(.*\)/\1/; 1q') else result=$(echo $ver | sed -e 's/\([0-9]*\)\(.*\)/\1/; 1q') fi fi done fi echo "$result" } _java="$(jdk_version)" echo $_java if [[ "$_java" > "$8" ]]; then echo version is more than 8 else echo version is less than 8 fi

来源

我希望能被证明是有帮助的< /em>

I had a similar problem, I wanted to check which version of java was installed to perform two types of application launches.
The following code has solved my problem

jdk_version() {
  local result
  local java_cmd
  if [[ -n $(type -p java) ]]
  then
    java_cmd=java
  elif [[ (-n "$JAVA_HOME") && (-x "$JAVA_HOME/bin/java") ]]
  then
    java_cmd="$JAVA_HOME/bin/java"
  fi
  local IFS=

source

I hope to be proved helpful

\n' # remove \r for Cygwin local lines=$("$java_cmd" -Xms32M -Xmx32M -version 2>&1 | tr '\r' '\n') if [[ -z $java_cmd ]] then result=no_java else for line in $lines; do if [[ (-z $result) && ($line = *"version \""*) ]] then local ver=$(echo $line | sed -e 's/.*version "\(.*\)"\(.*\)/\1/; 1q') # on macOS, sed doesn't support '?' if [[ $ver = "1."* ]] then result=$(echo $ver | sed -e 's/1\.\([0-9]*\)\(.*\)/\1/; 1q') else result=$(echo $ver | sed -e 's/\([0-9]*\)\(.*\)/\1/; 1q') fi fi done fi echo "$result" } _java="$(jdk_version)" echo $_java if [[ "$_java" > "$8" ]]; then echo version is more than 8 else echo version is less than 8 fi

source

I hope to be proved helpful

倾城泪 2024-12-10 06:40:18

这里有一个很好的便携式 bash 函数: http://eed3si9n.com/detecting-java -版本-bash

jdk_version() {
  local result
  local java_cmd
  if [[ -n $(type -p java) ]]
  then
    java_cmd=java
  elif [[ (-n "$JAVA_HOME") && (-x "$JAVA_HOME/bin/java") ]]
  then
    java_cmd="$JAVA_HOME/bin/java"
  fi
  local IFS=
\n'
  # remove \r for Cygwin
  local lines=$("$java_cmd" -Xms32M -Xmx32M -version 2>&1 | tr '\r' '\n')
  if [[ -z $java_cmd ]]
  then
    result=no_java
  else
    for line in $lines; do
      if [[ (-z $result) && ($line = *"version \""*) ]]
      then
        local ver=$(echo $line | sed -e 's/.*version "\(.*\)"\(.*\)/\1/; 1q')
        # on macOS, sed doesn't support '?'
        if [[ $ver = "1."* ]]
        then
          result=$(echo $ver | sed -e 's/1\.\([0-9]*\)\(.*\)/\1/; 1q')
        else
          result=$(echo $ver | sed -e 's/\([0-9]*\)\(.*\)/\1/; 1q')
        fi
      fi
    done
  fi
  echo "$result"
}

There is a nice portable bash function for this here: http://eed3si9n.com/detecting-java-version-bash

jdk_version() {
  local result
  local java_cmd
  if [[ -n $(type -p java) ]]
  then
    java_cmd=java
  elif [[ (-n "$JAVA_HOME") && (-x "$JAVA_HOME/bin/java") ]]
  then
    java_cmd="$JAVA_HOME/bin/java"
  fi
  local IFS=
\n'
  # remove \r for Cygwin
  local lines=$("$java_cmd" -Xms32M -Xmx32M -version 2>&1 | tr '\r' '\n')
  if [[ -z $java_cmd ]]
  then
    result=no_java
  else
    for line in $lines; do
      if [[ (-z $result) && ($line = *"version \""*) ]]
      then
        local ver=$(echo $line | sed -e 's/.*version "\(.*\)"\(.*\)/\1/; 1q')
        # on macOS, sed doesn't support '?'
        if [[ $ver = "1."* ]]
        then
          result=$(echo $ver | sed -e 's/1\.\([0-9]*\)\(.*\)/\1/; 1q')
        else
          result=$(echo $ver | sed -e 's/\([0-9]*\)\(.*\)/\1/; 1q')
        fi
      fi
    done
  fi
  echo "$result"
}
掌心的温暖 2024-12-10 06:40:18

我知道这是一个非常古老的线程,但这仍然对其他人有帮助。

要了解您运行的是 Java 5、6 还是 7,请首先输入 java -version

输出中将有一行看起来与此类似: java version "1.7.0_55"

然后,您使用此表将“行话”结果转换为版本号。

1.7.0_55 是 Java 7
1.6.0_75 是 Java 6
1.5.0_65 是 Java 5

信息取自 Oracle 站点上的页面
http://www.oracle.com/technetwork/java/javase /7u55-relnotes-2177812.html

I know this is a very old thread but this will still be of help to others.

To know whether you're running Java 5, 6 or 7, firstly type java -version.

There will be a line in your output that looks similar to this: java version "1.7.0_55"

Then you use this table for converting the 'jargon' result to the version number.

1.7.0_55 is Java 7
1.6.0_75 is Java 6
1.5.0_65 is Java 5

Information taken from a page on the Oracle site
http://www.oracle.com/technetwork/java/javase/7u55-relnotes-2177812.html

断舍离 2024-12-10 06:40:18

使用 bashj,扩展的 bash 版本 (https://sourceforge.net/projects/bashj/) ,您会得到一个相当紧凑的解决方案:

#!/usr/bin/bashj
echo System.getProperty("java.runtime.version")

答案由集成的 JVM 提供(脚本的执行时间约为 0.017 英寸,调用本身的执行时间约为 0.004 英寸)。

Using bashj, an extended bash version (https://sourceforge.net/projects/bashj/), you get a rather compact solution:

#!/usr/bin/bashj
echo System.getProperty("java.runtime.version")

The answer is provided by an integrated JVM (in ~0.017'' execution time for the script , and ~0.004'' for the call itself).

看海 2024-12-10 06:40:18

我发现以下作品相当不错。它是 Michał Šrajerjmj

version=$(java -version 2>&1 | sed -n ';s/.* 版本 "\(.*\)\.\(.*\)\..*"/\1\2/p;' | awk '{ print $1 }').

我安装了 openJDK 13,如果没有 awk 命令,第一个答案会打印:130 2020-01-14。因此,添加 awk '{ print $1 }' 只为我提供了版本。

I found the following works rather well. It is a combination of the answers from Michał Šrajer and jmj

version=$(java -version 2>&1 | sed -n ';s/.* version "\(.*\)\.\(.*\)\..*"/\1\2/p;' | awk '{ print $1 }').

I have openJDK 13 installed and without the awk command the first answer printed: 130 2020-01-14. Thus, adding awk '{ print $1 }' provides me with just the version.

欢你一世 2024-12-10 06:40:18

这个问题仍然相关,所以发布我的答案。

使用稍微复杂的方式,使用内置的 Bash 变量扩展并避免创建多个子 shell

line=$(java -version 2>&1 | head -1) && echo ${${${line#*\"}#1.}%%.*}

对于 Java 1.8.0_312 返回 8,对于 17.0.6 返回 17

这可以在如下脚本中使用

#!/bin/bash

required=5

version=$(line=$(java -version 2>&1 | head -1) && echo ${${${line#*\"}#1.}%%.*})

[[ $version -ge $required ]] && echo "Java version OK" || echo "Java version too low"

This question is still relevant so posting my answer.

Using slightly convoluted way by using built-in Bash variable expansions and avoiding creating multiple sub-shells

line=$(java -version 2>&1 | head -1) && echo ${${${line#*\"}#1.}%%.*}

Returns 8 for Java 1.8.0_312 and 17 for 17.0.6

This can be used in a script like below

#!/bin/bash

required=5

version=$(line=$(java -version 2>&1 | head -1) && echo ${${${line#*\"}#1.}%%.*})

[[ $version -ge $required ]] && echo "Java version OK" || echo "Java version too low"
凉风有信 2024-12-10 06:40:18

您可以使用 ps -ef | 获取进程 ID grep -v grep | grep -v grep java
然后使用 jcmd process_id vm.version

You can get the process ID by using ps -ef | grep -v grep | grep java
and then use jcmd process_id vm.version

雪落纷纷 2024-12-10 06:40:18

在 bash 中检查所需的最低 Java 版本的一行代码:

(echo 'class a{public static void main(String[] a){}}' > a.java && java --source 17 a.java || a=$? ; (rm -f a.java ; exit $a))

退出代码指示 Java 17+ 是否可用。请注意,这需要一个(临时)文件 a.java

One-liner to check for a minimum required Java version in bash:

(echo 'class a{public static void main(String[] a){}}' > a.java && java --source 17 a.java || a=$? ; (rm -f a.java ; exit $a))

The exitcode indicates if Java 17+ is available. Note that this requires a (temp) file a.java.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文