Changeset 18351 for gli/branches/rtl-gli/CheckJavaVersion.java
- Timestamp:
- 2009-01-12T11:04:15+13:00 (15 years ago)
- Location:
- gli/branches/rtl-gli
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
gli/branches/rtl-gli
-
Property svn:ignore
set to
jar
GLIServer.jar
GLI.jar
-
Property svn:ignore
set to
-
gli/branches/rtl-gli/CheckJavaVersion.java
r10757 r18351 1 public class CheckJavaVersion 2 { 3 static public void main(String[] args) 4 { 5 String java_version = System.getProperty("java.version"); 6 System.out.println("Java version: " + java_version); 1 import java.util.regex.Pattern; 7 2 8 try {9 // Identify major version number10 int major_dot_position = java_version.indexOf(".");11 String java_version_major_string = java_version.substring(0, major_dot_position);12 int java_version_major = (new Integer(java_version_major_string)).intValue();13 3 14 // Identify minor version number 15 int minor_dot_position = java_version.indexOf(".", major_dot_position + 1); 16 String java_version_minor_string = java_version.substring(major_dot_position + 1, minor_dot_position); 17 int java_version_minor = (new Integer(java_version_minor_string)).intValue(); 18 19 // Version of Java must be 1.4 or higher to run the GLI 20 if (java_version_major > 1 || (java_version_major == 1 && java_version_minor >= 4)) { 21 // Valid 22 System.exit(2); 23 } 24 else { 25 // Invalid 26 System.exit(1); 27 } 4 public 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 = "\\Q.\\E"; 46 String underscore = "\\Q_\\E"; 47 // Can't use Pattern.quote() since it is not there in Java 1.4.* 48 //String period = Pattern.quote("."); 49 //String underscore = Pattern.quote("_"); 50 51 String[] minVersionNums = minimumVersion.split(period+"|"+underscore); 52 String[] runningVersionNums =runningJavaVersion.split(period+"|"+underscore); 53 54 boolean acceptable = true; 55 // only keep looping while we haven't gone past the end of either array 56 int i=0; 57 for(; i < minVersionNums.length && i < runningVersionNums.length; i++) 58 { 59 int min = Integer.parseInt(minVersionNums[i]); 60 int run = Integer.parseInt(runningVersionNums[i]); 61 if(run > min) { 62 // certain success: one of the higher positional numbers 63 // of the running version is greater than the corresponding 64 // number of the minimum version, meaning we've finished. 65 break; 66 } else if(run < min) { 67 // fail: running version number is lower than corresponding 68 // minimum version number 69 acceptable = false; 70 break; 71 } 72 } 73 74 // Consider minVersion = 1.5.0_10 and runningVersion = 1.5.0 75 // this means the runningversion is still insufficient. 76 // HOWEVER, minVersion being longer does not always mean it is 77 // a later version, consider: min=1.5.0_9.12 and run=1.5.0_10 78 // This should be acceptable since 10 > 9 even though min is longer. 79 // SOLUTION: If the last values for both were the same, the running 80 // Version is not compatible if the minVersionNums array is longer 81 int min = Integer.parseInt(minVersionNums[i-1]); 82 int run = Integer.parseInt(runningVersionNums[i-1]); 83 84 // if the last values were the same, check whether min is longer 85 // in which case the running version is not acceptable 86 if(min == run && minVersionNums.length > runningVersionNums.length) 87 { 88 acceptable = false; 89 } 90 91 if(acceptable) { 92 System.out.println("Found compatible Java version " +runningJavaVersion); 93 System.exit(2); // acceptable case 94 } else { 95 System.out.println("The current Java version " + 96 runningJavaVersion + " is insufficient to run " + programName); 97 System.exit(1); 98 } 28 99 } 29 catch (Exception exception) {30 System.err.println("Exception: " + exception);31 }32 }33 100 }
Note:
See TracChangeset
for help on using the changeset viewer.