There are two ways to obtain the JVM version at runtime in Java:
java.version
.version()
method of the Runtime
class in Java.java.version
system propertyA system property is a key-value pair that stores properties specific to the current system.
The java.version
system property provides the JVM in use.
In the below code, we print the current Java version using the java.version
system property. We access the value of the property using the getProperty
method of the System
class.
public class Main {public static void main(String[] args){System.out.println("Current JVM version - " + System.getProperty("java.version"));}}
Runtime.version()
methodversion
is a static method of the Runtime
class that returns the current JVM version in use. This method was introduced in Java version 9.
We print the current Java version using the Runtime.version()
method in the below code.
public class Main {public static void main(String[] args){System.out.println("Current JVM version - " + Runtime.version());}}