Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

1. Overview

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.

2. Java Version Naming Convention

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.

3. Getting Java Version

3.1. Using 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:

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.

3.2. Using 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:

<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:

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.

3.3. Using 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:

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

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.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

Generic footer banner
Comments are closed on this article!