How to get Java version at runtime
Basically, there are two ways to get the version of the JVM at runtime of a Java program.
- Use the value of the java.version system property
- Use the Runtime.version() static method.
1. System.getProperty("java.version")
The System.getProperty("java.version") method returns a String describing the current version of the JVM.
Main1.java
package org.o7planning.java_14197_version;
public class Main1 {
public static void main(String[] args) {
String javaVersion = System.getProperty("java.version");
System.out.println("Current JVM version: " + javaVersion);
}
}
Output:
Current JVM version: 20.0.2
2. Runtime.version()
The Runtime.version() method returns a Version object, through this object you can know more information about the current version of the JVM.
Main2.java
package org.o7planning.java_14197_version;
import java.lang.Runtime.Version;
public class Main2 {
public static void main(String[] args) {
Version currentVersion = Runtime.version();
// Example: 20.0.2+9-78
System.out.println("Current JVM version: " + currentVersion.toString());
//
int feature = currentVersion.feature(); // 20
int interim = currentVersion.interim(); // 0
int update = currentVersion.update(); // 2
int patch = currentVersion.patch(); // 0
String pre = currentVersion.pre().isPresent() ? currentVersion.pre().get() : "";
Integer build = currentVersion.build().isPresent() ? currentVersion.build().get() : 0;// 9
String optional = currentVersion.optional().isPresent() ? currentVersion.optional().get() : ""; // 78
//
System.out.println(" - feature: " + feature);
System.out.println(" - interim: " + interim);
System.out.println(" - update: " + update);
System.out.println(" - patch: " + patch);
System.out.println(" - pre: " + pre);
System.out.println(" - build: " + build);
System.out.println(" - optional: " + optional);
//
String v17 = "17.0.8+9-LTS";
Version version17 = Version.parse(v17);
int value = currentVersion.compareTo(version17);
if (value == 0) {
System.out.println("Your Java Version is equal to " + v17);
} else if (value > 0) {
System.out.println("Your Java Version is newer than " + v17);
} else {
System.out.println("Your Java Version is older than " + v17);
}
}
}
Output:
Current JVM version: 20.0.2+9-78
- feature: 20
- interim: 0
- update: 2
- patch: 0
- pre:
- build: 9
- optional: 78
Your Java Version is newer than 17.0.8+9-LTS
The example below allows you to better understand the version format in Java:
ShowVersionInfo.java
package org.o7planning.java_14197_version;
import java.lang.Runtime.Version;
public class ShowVersionInfo {
public static void main(String[] args) {
String[] versionStrings = new String[] { //
"20.0.2+9-78", //
"17.0.8+9-LTS", //
"17.0.8.15+9-LTS", //
"17.0.8.15-X+9-LTS", //
};
for (String versionString : versionStrings) {
Version version = Version.parse(versionString);
System.out.println("JVM version: " + version.toString());
//
int feature = version.feature();
int interim = version.interim();
int update = version.update();
int patch = version.patch();
String pre = version.pre().isPresent() ? version.pre().get() : "";
Integer build = version.build().isPresent() ? version.build().get() : 0;
String optional = version.optional().isPresent() ? version.optional().get() : "";
//
System.out.println(" - feature: " + feature);
System.out.println(" - interim: " + interim);
System.out.println(" - update: " + update);
System.out.println(" - patch: " + patch);
System.out.println(" - pre: " + pre);
System.out.println(" - build: " + build);
System.out.println(" - optional: " + optional);
}
}
}
Output:
JVM version: 20.0.2+9-78
- feature: 20
- interim: 0
- update: 2
- patch: 0
- pre:
- build: 9
- optional: 78
JVM version: 17.0.8+9-LTS
- feature: 17
- interim: 0
- update: 8
- patch: 0
- pre:
- build: 9
- optional: LTS
JVM version: 17.0.8.15+9-LTS
- feature: 17
- interim: 0
- update: 8
- patch: 15
- pre:
- build: 9
- optional: LTS
JVM version: 17.0.8.15-X+9-LTS
- feature: 17
- interim: 0
- update: 8
- patch: 15
- pre: X
- build: 9
- optional: LTS
LTS: Long Term SupportJ2SE SDK/JRE Version String Naming Convention: