1. 概要

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

2. Springバージョンの入手方法

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

これを行うために、SpringVersionクラスのgetVersionメソッドを使用します

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

3. JDKバージョンの取得

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"));

または、Springクラスを使用せずにプロパティに直接アクセスすることもできます。

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

4. Javaバージョンの取得

Finally, we’ll see how to get the version of Java that our application is running on. この目的のために、クラスJavaVersionを使用します。

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

上記では、 JavaVersion#getJavaVersionメソッドを呼び出しています。 デフォルトでは、これはEIGHTなどの特定のJavaバージョンの列挙型を返します。 上記のメソッドと一貫性のあるフォーマットを維持するために、toStringメソッドを使用してフォーマットを解析します。

5. 結論

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 over on GitHub.