How to Check Java Version

Filed Under: Java

Sometimes we need to check the Java version while executing a java program. We could be developing our project in local machine but deploying at a remote server, so we need to verify java version to make sure our program will work fine.

How to Check Java Version?

We can easily check the java version using the command line. We can use any of the following commands to print the java version.


$java -version
$java --version

java -version will print version to the error stream and exit.

java --version will print its version to the output stream and exit.

Note that JDK or JRE bin directory should be in the PATH variable, otherwise you will get an error as java command not found.

I have Java 8, Java 9 and Java 10 installed on my MacBook, so let’s see what is the result of running java version check command.

Java Version 10 Output


$ java -version
java version "10.0.1" 2018-04-17
Java(TM) SE Runtime Environment 18.3 (build 10.0.1+10)
Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10.0.1+10, mixed mode)
$ 

Note that version is of the form $FEATURE.$INTERIM.$UPDATE.$PATCH, so from the above output, we can deduce that Feature is 10, Interim is 0, Update is 1 and Patch is 0.

In the second line, what you see after + is called build number. So the build is 10. Note that this naming convention has come up in Java 10 itself. So it will be slightly different in older java versions.

Java Version 9 Output


$ java -version
java version "9"
Java(TM) SE Runtime Environment (build 9+181)
Java HotSpot(TM) 64-Bit Server VM (build 9+181, mixed mode)
$

Java Version 8 Output


$ cd /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/bin/
$ ./java -version
java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)
$

Java Version 11 Output


$ java -version
openjdk version "11.0.1" 2018-10-16
OpenJDK Runtime Environment 18.9 (build 11.0.1+13)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.1+13, mixed mode)
$

Can we check java version programmatically?

What if we want to make sure our code is running on a compatible java version and maybe quit the application if it’s not the case. We can check the java version in the program also. Below code snippets show you how to check java version programmatically.

Java Version Check Code for Java 10


Version version = java.lang.Runtime.version();
System.out.println("Java Version = "+version);
System.out.println("Java Version Feature Element = "+version.feature());
System.out.println("Java Version Interim Element = "+version.interim());
System.out.println("Java Patch Element Version = "+version.patch());
System.out.println("Java Update Element Version = "+version.update());
System.out.println("Java Version Build = "+version.build().get());
System.out.println("Java Pre-Release Info = "+version.pre().orElse("NA"));

Output:
check java version


Java Version = 10.0.1+10
Java Version Feature Element = 10
Java Version Interim Element = 0
Java Patch Element Version = 0
Java Update Element Version = 1
Java Version Build = 10
Java Pre-Release Info = NA

Note that the program output matches with the output from java -version command.

Java Version Check Code for Java 9


Version version = java.lang.Runtime.version();
System.out.println("Java Version = "+version);
System.out.println("Java Major Version = "+version.major());
System.out.println("Java Minor Version = "+version.minor());
System.out.println("Java Security Version = "+version.security());
System.out.println("Java Version Build = "+version.build().get());
System.out.println("Java Version Pre-Release Info = "+version.pre().orElse("NA"));

Version class was first introduced in Java 9, in java 10 version major, minor and security properties were deprecated. If we execute above code snippet for Java 9, we get following output:


$ java JavaVersion
Java Version = 9+181
Java Major Version = 9
Java Minor Version = 0
Java Security Version = 0
Java Version Build = 181
Java Version Pre-Release Info = NA

Java Version Check Code for Java 8 or below versions

For Java 8 or below, we can use System properties to get the java version details.


System.out.println(System.getProperty("java.version"));
System.out.println(System.getProperty("java.specification.version"));

Output:


1.8.0_131
1.8
You can download the complete program code from our GitHub Repository

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages