Changeset 15232


Ignore:
Timestamp:
2008-04-29T10:30:57+12:00 (16 years ago)
Author:
ak19
Message:

Better comparison between minimum required Java Version and running Java Version. Does numberwise comparison.

Location:
other-projects/trunk/gs3-webservices-democlient
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • other-projects/trunk/gs3-webservices-democlient/CheckJavaVersion.java

    r15224 r15232  
     1import java.util.regex.Pattern;
     2
    13
    24public class CheckJavaVersion {
     
    1618        String programName = "this program";
    1719        // the version of java that's in use
    18         String installedJavaVersion = System.getProperty("java.version");
     20        String runningJavaVersion = System.getProperty("java.version");
    1921       
    2022        if(args.length > 0) {
     
    2628       
    2729        System.out.println("\nChecking for a compatible Java version..."
    28                 + "\nLooking for minimum version " + minimumVersion + ".x");
     30                + "\nLooking for minimum version: " + minimumVersion);
    2931       
    30         if(installedJavaVersion.compareTo(minimumVersion) < 0) {
    31             System.out.println(
    32                 "The current Java version "
    33                 + installedJavaVersion
    34                 + " is insufficient to run "
    35                 + programName
    36             );
     32        // Version numbers can be of the form "1.5.0_2"
     33        // We want to split version numbers into the individual numbers
     34        // For example: splitting 1.5.0_2 will give us {1,5,0,2},
     35        // while splitting 1.5.0_10 will give us {1,5,0,10}.
     36        // The comparison then is straightforward.
     37       
     38        // We will split version strings into the individual numbers
     39        // using regular expressions. However, the tokens . and _ are
     40        // reserved in regular expressions and need to be escaped:
     41        // Period: \Q.\E;  underscore: \Q_\E.
     42        // Once escaped, it should be indicated in the regular expression
     43        // that the two characters are separate tokens by using |, so
     44        // that the regex becomes: ".|_" -> \Q.\E|\Q_\E.
     45        String period = Pattern.quote(".");
     46        String underscore = Pattern.quote("_");
     47       
     48        String[] minVersionNums = minimumVersion.split(period+"|"+underscore);
     49        String[] runningVersionNums =runningJavaVersion.split(period+"|"+underscore);
     50       
     51        boolean acceptable = true;
     52        // only keep looping while we haven't gone past the end of either array
     53        for(int i=0; i < minVersionNums.length && i < runningVersionNums.length; i++)
     54        {
     55            int min = Integer.parseInt(minVersionNums[i]);
     56            int run = Integer.parseInt(runningVersionNums[i]);
     57            if(run < min) {
     58                // fail: running version number is lower than corresponding
     59                // minimum version number
     60                acceptable = false;
     61                break;
     62            }
     63        }
     64       
     65        if(acceptable) {
     66            System.out.println("Found compatible Java version " +runningJavaVersion);
     67            System.exit(0); // acceptable case
     68        } else {
     69            System.out.println("The current Java version " +
     70                runningJavaVersion + " is insufficient to run " + programName);
    3771            System.exit(-1);
    38         } else {
    39             System.out.println("Found compatible Java version "
    40                     + installedJavaVersion);
    41             System.exit(0);
    42         }
     72        }
    4373    }
    44 
    4574}
Note: See TracChangeset for help on using the changeset viewer.