X-Git-Url: http://source.jalview.org/gitweb/?p=jalview.git;a=blobdiff_plain;f=src%2Fjalview%2Futil%2FLaunchUtils.java;fp=src%2Fjalview%2Futil%2FLaunchUtils.java;h=5bd4a08491e0c9bba73e30223898a0455d37eb64;hp=f4d7e2795ac2c261a6104d3196c5af3a00db83c8;hb=53fe06e7ae7a8c00ec902413391cea15362bdbf2;hpb=a92455a4db78cdc97d94f7bddb1929096abab1f2 diff --git a/src/jalview/util/LaunchUtils.java b/src/jalview/util/LaunchUtils.java index f4d7e27..5bd4a08 100644 --- a/src/jalview/util/LaunchUtils.java +++ b/src/jalview/util/LaunchUtils.java @@ -24,6 +24,9 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; import java.util.Properties; public class LaunchUtils @@ -73,4 +76,114 @@ public class LaunchUtils { return Boolean.parseBoolean(getUserPreference(key)); } + + public static int JAVA_COMPILE_VERSION = 0; + + public static int getJavaCompileVersion() + { + if (Platform.isJS()) + { + return -1; + } + else if (JAVA_COMPILE_VERSION > 0) + { + return JAVA_COMPILE_VERSION; + } + String buildDetails = "jar:".concat(LaunchUtils.class + .getProtectionDomain().getCodeSource().getLocation().toString() + .concat("!" + "/.build_properties")); + try + { + URL localFileURL = new URL(buildDetails); + InputStream in = localFileURL.openStream(); + Properties buildProperties = new Properties(); + buildProperties.load(in); + in.close(); + String JCV = buildProperties.getProperty("JAVA_COMPILE_VERSION", + null); + if (JCV == null) + { + System.out.println( + "Could not obtain JAVA_COMPILE_VERSION for comparison"); + return -2; + } + JAVA_COMPILE_VERSION = Integer.parseInt(JCV); + } catch (MalformedURLException e) + { + System.err.println("Could not find " + buildDetails); + return -3; + } catch (IOException e) + { + System.err.println("Could not load " + buildDetails); + return -4; + } catch (NumberFormatException e) + { + System.err.println("Could not parse JAVA_COMPILE_VERSION"); + return -5; + } + + return JAVA_COMPILE_VERSION; + } + + public static int JAVA_VERSION = 0; + + public static int getJavaVersion() + { + if (Platform.isJS()) + { + return -1; + } + else if (JAVA_VERSION > 0) + { + return JAVA_VERSION; + } + try + { + String JV = System.getProperty("java.version"); + if (JV == null) + { + System.out.println("Could not obtain java.version for comparison"); + return -2; + } + if (JV.startsWith("1.")) + { + JV = JV.substring(2); + } + JAVA_VERSION = JV.indexOf(".") == -1 ? Integer.parseInt(JV) + : Integer.parseInt(JV.substring(0, JV.indexOf("."))); + } catch (NumberFormatException e) + { + System.err.println("Could not parse java.version"); + return -3; + } + return JAVA_VERSION; + } + + public static boolean checkJavaVersion() + { + if (Platform.isJS()) + { + return true; + } + String buildDetails = "jar:".concat(LaunchUtils.class + .getProtectionDomain().getCodeSource().getLocation().toString() + .concat("!" + "/.build_properties")); + + int java_compile_version = getJavaCompileVersion(); + int java_version = getJavaVersion(); + + if (java_compile_version <= 0 || java_version <= 0) + { + System.out.println("Could not make Java version check"); + return true; + } + // Warn if these java.version and JAVA_COMPILE_VERSION conditions exist + // Usually this means a Java 11 compiled JAR being run by a Java 11 JVM + if (java_version >= 11 && java_compile_version < 11) + { + return false; + } + + return true; + } }