Home
Categories
Dictionary
Download
Project Details
Changes Log
FAQ
License

JREVersionDetector



The JREVersionDetector class allows to get the version of a JRE at a specified location, or compare this version with one or two specified version numbers. This class works by trying to start a JVM as an external process with the following command: java -version.

It is also possible to:
  • Specify a timeOut for launching the JVM as an external process (the default timeOut is 200 ms)
  • Specify a constraint on the JVM architecture (32 bits or 64 bits)

The methods of this class will work by starting a process using the Java executable set at the JRE location, and call internally java -version to get the Java process characteristics.

Setting the JRE location

The methods of this class need the location of the JRE as File. The File which is expected is the java.home directory of the JRE to check. The Java executable will be looked for under the <java.home>/bin directory.

This class does not work with the current JRE (ie the JRE used to execute the program), but for any JRE which is on the disk. For example, you may use this class with a Java 20 JRE on a 64 bit architecture, but check the version and the architecture of a Java 8 JRE on a 32 bits architecture.

Setting the timeOut

The JREVersionDetector.setDefaultTimeOut(long) allow to set the default timeOut for subsequent instances of the class.

The JREVersionDetector.setTimeOut(long) allow to set the timeOut for the current instance of the class.

For example:
   JREVersionDetector detector = new JREVersionDetector();
   // the timeOut will be the default timeOut of 200 ms
   detector.isAtLeastVersion(new File(<my JRE Location>), "1.7");

   JREVersionDetector.setDefaultTimeOut(500L);
   // the timeOut will be the new default timeOut of 500 ms
   detector.isAtLeastVersion(new File(<my JRE Location>), "1.7");

   JREVersionDetector detector2 = new JREVersionDetector();
   detector2.setTimeOut(600L);
   // the timeOut will be 600 ms, the default timeOut has not changed
   detector.isAtLeastVersion(new File(<my JRE Location>), "1.7");

Examples

  JREVersionDetector detector = new JREVersionDetector();
  // return true if the version is at least 1.7
  detector.isAtLeastVersion(new File(<my JRE Location>), "1.7");

  // return true if the version is between 1.7 and 1.8
  detector.isBetweenVersions(new File(<my JRE Location>), "1.7", "1.8", true);

  // return true if the version is between 1.7 and 1.8, and apply a timeOut
  detector.isBetweenVersions(new File(<my JRE Location>), "1.7", "1.8", true);

  // return true if the version is between 1.7 and 1.8, on a 32 bit architecture
  detector.isBetweenVersions(new File(<my JRE Location>), "1.7", "1.8", JREVersionDetector.32_BIT, true);
      
  // get the characteristics of the JRE, will allow to get the version and the architecture
  JREVersionDetector.JREConfig config = detector.getJREConfiguration(new File(<my JRE Location>));      

Categories: lang | packages

Copyright 2006-2024 Herve Girod. All Rights Reserved. Documentation and source under the LGPL v2 and Apache 2.0 licences