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:
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:
Alternatively, we can access the property directly without using that Spring class:
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:
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.
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.