Java version comparison

I recently was given a task to allow only certain versions of Java to run an application. So, for example, we could say that our app requires Java 7 and won’t probably work with new upcoming Java 9 releases (to be on the safe side). We could also define some specific versions of Java which have known issues or bugs. Then we could show a warning message or even quit an application.

In order to get a version of Java you are currently using to run an application you can inspect a system property: java.version. What it returns is a product version. Starting from Java 1.3.0 its format is as follows:

  • The format should be numeric and in lower case.
  • A period “.” separator indicates a major version.
  • An underbar “_” separator is used to indicate an update release.
  • A dash “-” separator is used to indicate early access, beta and candidate releases.
  • All versions are ordered based on the standard dot-notation (1.3.0 < 1.3.0_01 < 1.3.1 < 1.3.1_01).

To simplify version matching I created a simple helper class which you can use to know if a given Java version satisfies the criteria using a simple expression language:

  • A version, when it is specified without a prefix, is matched exactly (i.e. 1.7.0).
  • If a version is prefixed with comparison operator (<, >, <=, >=, <>) it will be exaluated to true or false.
  • Boolean operators (&, |, !) and parentheses can be used to combine several expressions together.

A couple of examples are given below:

Here’s the URL for the project: https://github.com/vtsaplin/java-version-matcher

Share on FacebookShare on LinkedInShare on Google+Tweet about this on TwitterPin on PinterestShare on RedditDigg this

Vitaly Tsaplin

Hey, I am Vitaly and this is my blog. I live in Basel and work at Adobe.

More Posts - Website

Follow Me:
TwitterFacebookLinkedInGoogle Plus

Leave a Comment

Time limit is exhausted. Please reload CAPTCHA.