Getting the Java Version at Runtime – 在运行时获取Java版本

最后修改: 2021年 5月 13日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

Sometimes when programming in Java, it may be helpful to programmatically find the version of Java that we’re using. In this tutorial, we’ll look at a few ways to get the Java version.

有时在用Java编程时,以编程方式找到我们所使用的Java的版本可能会有帮助。在本教程中,我们将了解一些获取Java版本的方法。

2. Java Version Naming Convention

2.Java版本的命名规则

Up until Java 9, the Java version did not follow the Semantic Versioning. The format was 1.X.Y_ZX and Y indicate major and minor versions, respectively. Z is used to indicate an update release and separated by underscore “_”. For example, 1.8.0_181. For Java 9 and beyond, the Java version follows the Semantic Versioning. The Semantic Versioning uses the X.Y.Z format. It refers to major, minor, and patch. For example, 11.0.7.

直到Java 9,Java版本没有遵循Semantic Versioning>。其格式是1.X.Y_ZXY分别表示主要和次要版本。Z用于表示更新版本,用下划线”_”分隔。例如,1.8.0_181。对于Java 9及以后的版本,Java版本遵循Semantic Versioning。Semantic Versioning使用X.Y.Z格式。它指的是主要、次要和补丁。例如,11.0.7

3. Getting Java Version

3.获取Java版本

3.1. Using System.getProperty

3.1.使用System.getProperty

A system property is a key-value pair that the Java runtime provides. The java.version is a system property that provides the Java version. Let’s define a method for getting the version:

系统属性是Java运行时提供的一个键值对。java.version是一个系统属性,提供了Java的版本。我们来定义一个获取版本的方法。

public void givenJava_whenUsingSystemProp_thenGetVersion() {
    int expectedVersion = 8;
    String[] versionElements = System.getProperty("java.version").split("\\.");
    int discard = Integer.parseInt(versionElements[0]);
    int version;
    if (discard == 1) {
        version = Integer.parseInt(versionElements[1]);
    } else {
        version = discard;
    }
    Assertions.assertThat(version).isEqualTo(expectedVersion);
}

To support both Java version formats, we should check the first number until the dot.

为了支持两种Java 版本格式,我们应该检查第一个数字,直到点。

3.2. Using Apache Commons Lang 3

3.2.使用Apache Commons Lang 3

A second approach for getting the Java version is via the Apache Commons Lang 3 library. We first need to add the commons-lang3 Maven dependency to our pom.xml file:

获取Java版本的第二个方法是通过Apache Commons Lang 3库。我们首先需要将commons-lang3 Maven依赖性添加到我们的pom.xml文件。

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.12.0</version>
</dependency>

We’ll use the SystemUtils class to obtain information about the Java platform. Let’s define a method for this purpose:

我们将使用SystemUtils类来获取Java平台的信息。让我们为这个目的定义一个方法。

public void givenJava_whenUsingCommonsLang_thenGetVersion() {
    int expectedVersion = 8;
    String[] versionElements = SystemUtils.JAVA_SPECIFICATION_VERSION.split("\\.");
    int discard = Integer.parseInt(versionElements[0]);
    int version;
    if (discard == 1) {
        version = Integer.parseInt(versionElements[1]);
    } else {
        version = discard;
    }
    Assertions.assertThat(version).isEqualTo(expectedVersion);
}

We are using the JAVA_SPECIFICATION_VERSION that is intended to mirror available values from the java.specification.version System property.

我们正在使用 JAVA_SPECIFICATION_VERSION,它旨在反映来自 java.specification.version 系统属性的可用值。

3.3. Using Runtime.version()

3.3.使用Runtime.version()

In Java 9 and above, we can use the Runtime.version() to get version information. Let’s define a method for this purpose:

在Java 9及以上版本中,我们可以使用Runtime.version()来获取版本信息。让我们为这个目的定义一个方法。

public void givenJava_whenUsingRuntime_thenGetVersion(){
    String expectedVersion = "15";
    Runtime.Version runtimeVersion = Runtime.version();
    String version = String.valueOf(runtimeVersion.version().get(0));
    Assertions.assertThat(version).isEqualTo(expectedVersion);
}

4. Conclusion

4.总结

In this article, we describe a few ways to obtain the Java version. As usual, all code samples used in this tutorial are available over on GitHub.

在这篇文章中,我们描述了几种获取Java版本的方法。像往常一样,本教程中使用的所有代码样本都可以在GitHub上找到