Partner – Microsoft – NPI (cat= Spring)
announcement - icon

Azure Spring Apps is a fully managed service from Microsoft (built in collaboration with VMware), focused on building and deploying Spring Boot applications on Azure Cloud without worrying about Kubernetes.

And, the Enterprise plan comes with some interesting features, such as commercial Spring runtime support, a 99.95% SLA and some deep discounts (up to 47%) when you are ready for production.

>> Learn more and deploy your first Spring Boot app to Azure.

You can also ask questions and leave feedback on the Azure Spring Apps GitHub page.

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.

Course – LS (cat=Spring)

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

res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.