How to get java version programatically

Why java version at runtime
Sometimes you need to execute some code conditionally based on the current executing java version. For example, there is some functionality whose code was different on an earlier java release but on later releases it was optimized as per the new features introduced in java itself.

This requires that you determine the version of java during program execution and execute code according to it. This post will list down different ways in which this is possible
Using java.version system property
Java provides a property java.version which returns the version of java runtime, that is, the environment in which the current code is executing. Example,

public class JavaVersionDemo {
   public static void main(String[] args) {
      String version = System.getProperty("java.version");
      System.out.print("Java version is : " + version);
   }
}

Above code produces following output

Java version is : 1.8.0_51

This means that the current java version is 1.8.0 and an update of 51. Value separated with an underscore represents an update release.

Remember that an underscore will only be present on java versions 8 and below. Versions higher than 8 have a plus sign(+) to signify an update.

Using java.runtime.version system property
Value of this property includes the product version and an identifier of product build. This identifier is separated from product version by a “-“(hyphen). Example,

public class JavaVersionDemo {
   public static void main(String[] args) {
      String version = System.getProperty("java.runtime.version");
      System.out.print("Java version is : " + version);
   }
}

Above code produces following output

Java version is : 1.8.0_51-b16

This means that the current java version is 1.8.0 with an update of 51 and its build number is b16.
Value separated with an underscore represents an update release while that separated with a hyphen(-) is the build number.
For java versions 8 and above, underscore is replaced with a +. Thus, above code when executed on Java 11, produces below result.

Java version is : 11.0.2+9

Using java.lang.Runtime.Version class
Java 9 introduced Version which is a static nested class of java.lang.Runtime and provides the version details of the java runtime enviroment.
This class can be used to extract the version information at runtime or during execution. In order to get an instance of this class, use static method version() of java.lang.Runtime class.
Version value returned by this class can be divided into 4 numeric values separated by dots(.) as

FEATURE.INTERIM.UPDATE.PATCH

where,
Feature is the main release and contains new features. This value can be retrieved by using feature method of Runtime.Version.
Interim contains the bug fixes and enhancements. interim method of Runtime.Version returns this value.
Update means that it contains security issue fixes and bug fixes. This value can be retrieved by using update method of Runtime.Version.
Patch contains emergency fixes for critical bugs. There is a patch method in Runtime.Version to get this value.

public class JavaVersionDemo {
   public static void main(String[] args) {
     // get version instance
     Version version = Runtime.version();
     System.out.println("Runtime version");
     // print string representation of version
     System.out.println(version.toString()); 
     System.out.println("Creating version using individual components");
     // get individual version components and join them
     System.out.println(version.feature() + "." + version.interim() + "." 
             + version.update() + "." + version.patch());
   }
}

Above code produces below output

Runtime version
11.0.2+9
Creating version using individual components
11.0.2.0

where +9 means 9 is the update version of 11.0.2 release.
Hope this post was helpful. Hit the clap below to like the post.

0

Leave a Reply

Mark Your Impression

  Subscribe  
Notify of
Close Menu

Never Miss an article !

Get the new post delivered straight into your inbox, enter your email and hit the button

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

codippa will use the information you provide on this form to be in touch with you and to provide updates and marketing.