How to Check Java Version With Examples

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 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

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 8 Output

Java Version 11 Output

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

Output:

check-java-version

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

Java Version Check Code for Java 9

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 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.


Output:

By admin

Leave a Reply