1. Overview

In this tutorial, we’ll demonstrate how to programmatically find out which version of Spring, JDK, and Java our application is using.

2. How to Get Spring Version

We’ll start by learning how to obtain the version of Spring that our application is using.

In order to do this, we’ll use the getVersion method of the SpringVersion class:

assertEquals("5.1.10.RELEASE", SpringVersion.getVersion());

3. Getting JDK Version

Next, we’ll get the JDK version that we’re currently using in our project. It’s important to note that Java and the JDK aren’t the same thing, so they’ll have different version numbers.

If we’re using Spring 4.x, there’s a class called JdkVersion, which we can use to get this information. However, this class was removed from Spring 5.x, so we’ll have to take that into account and work around it.

Internally, the Spring 4.x JdkVersion class was getting the version from the SystemProperties class, so we can do the same. Making use of the class SystemProperties, we’ll access the property java.version:

assertEquals("1.8.0_191", SystemProperties.get("java.version"));

Alternatively, we can access the property directly without using that Spring class:

assertEquals("1.8.0_191", System.getProperty("java.version"));

4. Obtaining Java Version

Finally, we’ll see how to get the version of Java that our application is running on. For this purpose, we’ll use the class JavaVersion:

assertEquals("1.8", JavaVersion.getJavaVersion().toString());

Above, we call the JavaVersion#getJavaVersion method. By default, this returns an enum with the specific Java version, such as EIGHT. To keep the formatting consistent with the above methods, we parse it using its toString method.

5. Conclusion

In this article, we learned that it’s quite simple to obtain the versions of Spring, JDK, and Java that our application is using.

As always, the complete code is available on GitHub.