In the previous guide, we learned how to check java version using command line. In this guide, we will write a Java program to check the java version. To do this, we can use the System
class, which gives access to the system properties.
Program to check Java version
public class JavaVersionCheck {
public static void main(String[] args) {
// Get the Java version
String javaVersion = System.getProperty("java.version");
// Print the Java version
System.out.println("Java Version: " + javaVersion);
}
}
Output:
Java Version: 1.8.0_291
Here 1.8.0_291
indicates Java version 8, update 291.
Explanation
System.getProperty("java.version")
retrieves the java version from the system. This checks which version of java is currently running on the system and returns the version details.System.out.println
prints the retrieved Java version.
Leave a Reply