source: gli/trunk/CheckJavaVersion.java@ 15354

Last change on this file since 15354 was 15310, checked in by ak19, 16 years ago

New CheckJavaVersion that works with a default minimum required Java version of 1.4 but which will accept a specific minimum required version as the first cmd-line arg. It still returns 2 if the running Java version is acceptable and 1 if this is lower than the minimum.

  • Property svn:keywords set to Author Date Id Revision
File size: 3.4 KB
Line 
1import java.util.regex.Pattern;
2
3
4public class CheckJavaVersion {
5 static final String MINIMUM_VERSION_PREFIX = "1.4";
6
7 /**
8 * @param args, arg[0] is the minium version of Java required
9 * to run the program. arg[1] is the name of the program.
10 * If arg[1] is left out, then no distinct program name is
11 * mentioned. If arg[0] is left out as well, then Greenstone3's
12 * minimum default version of 1.4.x is assumed.
13 * The program exits with 1 if the Java version being used is
14 * incompatible and with 2 if it is acceptable.
15 */
16 public static void main(String[] args) {
17 String minimumVersion = MINIMUM_VERSION_PREFIX;
18 String programName = "this program";
19 // the version of java that's in use
20 String runningJavaVersion = System.getProperty("java.version");
21
22 if(args.length > 0) {
23 minimumVersion = args[0];
24 }
25 if(args.length > 1) {
26 programName = args[1];
27 }
28
29 System.out.println("\nChecking for a compatible Java version..."
30 + "\nLooking for minimum version: " + minimumVersion);
31
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 int i=0;
54 for(; i < minVersionNums.length && i < runningVersionNums.length; i++)
55 {
56 int min = Integer.parseInt(minVersionNums[i]);
57 int run = Integer.parseInt(runningVersionNums[i]);
58 if(run < min) {
59 // fail: running version number is lower than corresponding
60 // minimum version number
61 acceptable = false;
62 break;
63 }
64 }
65
66 // Consider minVersion = 1.5.0_10 and runningVersion = 1.5.0
67 // this means the runningversion is still insufficient.
68 // HOWEVER, minVersion being longer does not always mean it is
69 // a later version, consider: min=1.5.0_9.12 and run=1.5.0_10
70 // This should be acceptable since 10 > 9 even though min is longer.
71 // SOLUTION: If the last values for both were the same, the running
72 // Version is not compatible if the minVersionNums array is longer
73 int min = Integer.parseInt(minVersionNums[i-1]);
74 int run = Integer.parseInt(runningVersionNums[i-1]);
75
76 // if the last values were the same, check whether min is longer
77 // in which case the running version is not acceptable
78 if(min == run && minVersionNums.length > runningVersionNums.length)
79 {
80 acceptable = false;
81 }
82
83 if(acceptable) {
84 System.out.println("Found compatible Java version " +runningJavaVersion);
85 System.exit(2); // acceptable case
86 } else {
87 System.out.println("The current Java version " +
88 runningJavaVersion + " is insufficient to run " + programName);
89 System.exit(1);
90 }
91 }
92}
Note: See TracBrowser for help on using the repository browser.