Spring Top

Get started with Spring 5 and Spring Boot 2, through the reference Learn Spring course:

>> LEARN SPRING
Java Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

In this article, we're going to show how to programmatically find out which version of Spring, JDK, and Java our application is using.

2. How to Get Spring Version

Let's 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, let's get the JDK version that is currently used in our project. It's important to note that Java and the JDK are not the same thing, so they'll have different version numbers.

If we're using Spring 4.x, there is a class called JdkVersion that can be used to get this information. However, this class was removed from Spring 5.x – so let's take that into account and work around it.

Internally, the Spring 4.x JdkVersion class was getting the version from the SystemProperties class, so let's do the same. Making use of the class SystemProperties, let's 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, let's 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've learned that it's quite simple to obtain the versions of Spring, JDK, and Java that our application is using.

As always, you can find the code over on GitHub.

Spring bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> THE COURSE
Java bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
Junit footer banner
Comments are closed on this article!