How To Check Java Installed Or Not
Sometimes nosotros need to bank check the Coffee version while executing a java programme. We could be developing our project in local car but deploying at a remote server, then nosotros need to verify java version to make certain our program will work fine.
How to Check Java Version?
We tin can easily check the java version using the command line. We can use any of the following commands to impress the java version.
$java -version $java --version
java -version
will print version to the fault stream and leave.
coffee --version
will print its version to the output stream and leave.
Note that JDK or JRE bin directory should be in the PATH variable, otherwise you volition get an fault as java command not plant
.
I have Java eight, Java 9 and Java 10 installed on my MacBook, so let'due south see what is the result of running coffee version check command.
Coffee Version ten Output
$ java -version coffee version "10.0.1" 2022-04-17 Java(TM) SE Runtime Surroundings 18.3 (build 10.0.1+10) Coffee HotSpot(TM) 64-Scrap Server VM 18.3 (build 10.0.1+10, mixed mode) $
Notation that version is of the form $FEATURE.$INTERIM.$UPDATE.$PATCH
, so from the in a higher place output, nosotros tin deduce that Feature is 10, Acting is 0, Update is ane and Patch is 0.
In the second line, what you lot come across after + is called build number. So the build is ten. Notation that this naming convention has come up upwardly in Java 10 itself. So it will be slightly different in older java versions.
Java Version nine Output
$ java -version java version "9" Java(TM) SE Runtime Environs (build ix+181) Coffee HotSpot(TM) 64-Fleck Server VM (build 9+181, mixed mode) $
Java Version viii Output
$ cd /Library/Java/JavaVirtualMachines/jdk1.eight.0_131.jdk/Contents/Dwelling house/bin/ $ ./coffee -version java version "i.8.0_131" Java(TM) SE Runtime Environment (build i.8.0_131-b11) Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode) $
Java Version 11 Output
$ coffee -version openjdk version "11.0.1" 2022-10-16 OpenJDK Runtime Environment 18.ix (build 11.0.1+xiii) OpenJDK 64-Scrap Server VM eighteen.9 (build 11.0.i+13, mixed mode) $
Can we bank check java version programmatically?
What if nosotros want to make certain our code is running on a uniform java version and maybe quit the application if it's not the case. We can check the coffee version in the program also. Below lawmaking snippets show you how to check java version programmatically.
Java Version Check Lawmaking for Java 10
Version version = java.lang.Runtime.version(); System.out.println("Java Version = "+version); System.out.println("Java Version Feature Element = "+version.feature()); Arrangement.out.println("Coffee Version Interim Element = "+version.interim()); Organization.out.println("Java Patch Element Version = "+version.patch()); Organisation.out.println("Coffee Update Element Version = "+version.update()); Organization.out.println("Java Version Build = "+version.build().get()); Organisation.out.println("Java Pre-Release Info = "+version.pre().orElse("NA"));
Output:
Java Version = 10.0.1+10 Java Version Characteristic Element = 10 Java Version Interim Element = 0 Coffee Patch Element Version = 0 Java Update Element Version = ane Java Version Build = 10 Coffee Pre-Release Info = NA
Note that the program output matches with the output from coffee -version
command.
Coffee Version Check Code for Java 9
Version version = java.lang.Runtime.version(); Arrangement.out.println("Java Version = "+version); System.out.println("Coffee Major Version = "+version.major()); System.out.println("Java Small-scale Version = "+version.minor()); System.out.println("Java Security Version = "+version.security()); Organization.out.println("Java Version Build = "+version.build().become()); System.out.println("Java Version Pre-Release Info = "+version.pre().orElse("NA"));
Version
form was showtime introduced in Coffee 9, in java 10 version major, minor and security properties were deprecated. If we execute above lawmaking snippet for Java 9, nosotros go post-obit output:
$ coffee JavaVersion Coffee Version = nine+181 Java Major Version = 9 Coffee Small Version = 0 Java Security Version = 0 Coffee Version Build = 181 Coffee Version Pre-Release Info = NA
Java Version Check Code for Java 8 or beneath versions
For Java 8 or beneath, nosotros can use System properties to become the java version details.
System.out.println(System.getProperty("java.version")); System.out.println(System.getProperty("coffee.specification.version"));
Output:
i.8.0_131 1.8
You lot can download the complete program code from our GitHub Repository
Source: https://www.journaldev.com/20930/check-java-version
Posted by: mcraeopoetinat.blogspot.com
0 Response to "How To Check Java Installed Or Not"
Post a Comment