From 7dcec14f584c3b4fc95e316e3d91e7d452889e5c Mon Sep 17 00:00:00 2001 From: Ben Soares Date: Thu, 7 Apr 2022 00:08:01 +0100 Subject: [PATCH] JAL-3991 JAVA_COMPILE_VERSION put into build_properties. Check for possible JVM mismatch in jalview.bin.LaunchUtils. STDERR warning from jalview.bin.Launcher. Console and optional popup warning in jalview.bin.Jalview. --- build.gradle | 1 + resources/lang/Messages.properties | 3 + resources/lang/Messages_es.properties | 4 ++ src/jalview/bin/Cache.java | 5 ++ src/jalview/bin/Jalview.java | 41 ++++++++++++ src/jalview/bin/Launcher.java | 11 +++- src/jalview/util/LaunchUtils.java | 113 +++++++++++++++++++++++++++++++++ 7 files changed, 176 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index b4636bc..9b7f5b9 100644 --- a/build.gradle +++ b/build.gradle @@ -1270,6 +1270,7 @@ task createBuildProperties(type: WriteProperties) { property "BUILD_DATE", getDate("HH:mm:ss dd MMMM yyyy") property "VERSION", JALVIEW_VERSION property "INSTALLATION", INSTALLATION+" git-commit:"+gitHash+" ["+gitBranch+"]" + property "JAVA_COMPILE_VERSION", JAVA_INTEGER_VERSION if (getdownSetAppBaseProperty) { property "GETDOWNAPPBASE", getdownAppBase property "GETDOWNAPPDISTDIR", getdownAppDistDir diff --git a/resources/lang/Messages.properties b/resources/lang/Messages.properties index 9ce5a63..19ec965 100644 --- a/resources/lang/Messages.properties +++ b/resources/lang/Messages.properties @@ -1332,6 +1332,7 @@ label.backupfiles_confirm_save_file_backupfiles_roll_wrong = Something possibly label.backupfiles_confirm_save_new_saved_file_ok = The new saved file seems okay. label.backupfiles_confirm_save_new_saved_file_not_ok = The new saved file might not be okay. label.continue_operation = Continue operation? +label.continue = Continue label.backups = Backups label.backup = Backup label.backup_files = Backup Files @@ -1412,3 +1413,5 @@ label.maximum_memory_tooltip = Enter memory as an integer number optionally foll label.adjustments_for_this_computer = Adjustments for this computer label.memory_example_text = Maximum memory that would be used with these settings on this computer label.memory_example_tooltip = The memory allocated to Jalview is the smaller of the percentage of physical memory (default 90%) and the maximum absolute memory (default 32GB). If your computer's memory cannot be ascertained then the maximum absolute memory defaults to 8GB (if not customised).
Jalview will always try and reserve 512MB for the OS and at least 512MB for itself. +warning.wrong_jvm_version_title = Wrong Java Version +warning.wrong_jvm_version_message = The Java version being used (Java {0}) may lead to problems.\nThis installation of Jalview should be used with Java {1}. diff --git a/resources/lang/Messages_es.properties b/resources/lang/Messages_es.properties index fb87f7d..089a91e 100644 --- a/resources/lang/Messages_es.properties +++ b/resources/lang/Messages_es.properties @@ -1322,6 +1322,7 @@ label.backupfiles_confirm_save_file_backupfiles_roll_wrong = Posiblemente algo e label.backupfiles_confirm_save_new_saved_file_ok = El nuevo archivo guardado parece estar bien. label.backupfiles_confirm_save_new_saved_file_not_ok = El nuevo archivo guardado podría no estar bien. label.continue_operation = ¿Continuar operación? +label.continue = Continua label.backups = Respaldos label.backup = Respaldo label.backup_files = Archivos de respaldos @@ -1402,3 +1403,6 @@ label.maximum_memory_tooltip = Ingrese la memoria como un n label.adjustments_for_this_computer = Ajustes para esta computadora label.memory_example_text = Memoria máxima que se usaría con esta configuración en esta computadora label.memory_example_tooltip = La memoria asignada a Jalview es el menor entre el porcentaje de memoria física (predeterminado 90%) y la memoria absoluta máxima (predeterminado 32 GB). Si no se puede determinar la memoria de su computadora, la memoria absoluta máxima predeterminada es de 8 GB (si no está personalizada).
Jalview siempre intentará reservar 512 MB para el sistema operativo y al menos 512 MB para sí mismo. +warning.wrong_jvm_version_title = Versión incorrecta de Java +warning.wrong_jvm_version_message = La versión de Java que se está utilizando (Java {0}) puede generar problemas.\nEsta instalación de Jalview debe usarse con Java {1}. + diff --git a/src/jalview/bin/Cache.java b/src/jalview/bin/Cache.java index ac8a183..9d3152e 100755 --- a/src/jalview/bin/Cache.java +++ b/src/jalview/bin/Cache.java @@ -627,6 +627,11 @@ public class Cache applicationProperties.put("VERSION", buildProperties.getProperty("VERSION")); } + if (buildProperties.getProperty("JAVA_COMPILE_VERSION", null) != null) + { + applicationProperties.put("JAVA_COMPILE_VERSION", + buildProperties.getProperty("JAVA_COMPILE_VERSION")); + } } catch (Exception ex) { System.out.println("Error reading build details: " + ex); diff --git a/src/jalview/bin/Jalview.java b/src/jalview/bin/Jalview.java index fc4c821..9574e7e 100755 --- a/src/jalview/bin/Jalview.java +++ b/src/jalview/bin/Jalview.java @@ -44,6 +44,7 @@ import java.util.logging.ConsoleHandler; import java.util.logging.Level; import java.util.logging.Logger; +import javax.swing.JOptionPane; import javax.swing.UIManager; import javax.swing.UIManager.LookAndFeelInfo; @@ -72,6 +73,7 @@ import jalview.schemes.ColourSchemeI; import jalview.schemes.ColourSchemeProperty; import jalview.util.ChannelProperties; import jalview.util.HttpUtils; +import jalview.util.LaunchUtils; import jalview.util.MessageManager; import jalview.util.Platform; import jalview.ws.jws2.Jws2Discoverer; @@ -429,6 +431,35 @@ public class Jalview * @j2sIgnore */ { + + /** + * Check to see that the JVM version being run is suitable for the Java + * version this Jalview was compiled for. Popup a warning if not. + */ + if (!LaunchUtils.checkJavaVersion()) + { + Console.warn("The Java version being used (Java " + + LaunchUtils.getJavaVersion() + + ") may lead to problems. This installation of Jalview should be used with Java " + + LaunchUtils.getJavaCompileVersion() + "."); + + if (!LaunchUtils + .getBooleanUserPreference("IGNORE_JVM_WARNING_POPUP")) + { + Object[] options = { + MessageManager.getString("label.continue") }; + JOptionPane.showOptionDialog(null, + MessageManager.formatMessage( + "warning.wrong_jvm_version_message", + LaunchUtils.getJavaVersion(), + LaunchUtils.getJavaCompileVersion()), + MessageManager + .getString("warning.wrong_jvm_version_title"), + JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, + null, options, options[0]); + } + } + if (!aparser.contains("nowebservicediscovery")) { desktop.startServiceDiscovery(); @@ -490,6 +521,16 @@ public class Jalview } } + // Check if JVM and compile version might cause problems and log if it + // might. + if (headless && !Platform.isJS() && !LaunchUtils.checkJavaVersion()) + { + Console.warn("The Java version being used (Java " + + LaunchUtils.getJavaVersion() + + ") may lead to problems. This installation of Jalview should be used with Java " + + LaunchUtils.getJavaCompileVersion() + "."); + } + // Move any new getdown-launcher-new.jar into place over old // getdown-launcher.jar String appdirString = System.getProperty("getdownappdir"); diff --git a/src/jalview/bin/Launcher.java b/src/jalview/bin/Launcher.java index 1ea17d6..a55146d 100644 --- a/src/jalview/bin/Launcher.java +++ b/src/jalview/bin/Launcher.java @@ -20,13 +20,12 @@ */ package jalview.bin; -import java.util.Locale; - import java.io.File; import java.io.IOException; import java.lang.management.ManagementFactory; import java.util.ArrayList; import java.util.List; +import java.util.Locale; import jalview.util.ChannelProperties; import jalview.util.LaunchUtils; @@ -63,6 +62,14 @@ public class Launcher */ public static void main(String[] args) { + if (!LaunchUtils.checkJavaVersion()) + { + System.err.println("WARNING - The Java version being used (Java " + + LaunchUtils.getJavaVersion() + + ") may lead to problems. This installation of Jalview should be used with Java " + + LaunchUtils.getJavaCompileVersion() + "."); + } + final String javaBin = System.getProperty("java.home") + File.separator + "bin" + File.separator + "java"; 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; + } } -- 1.7.10.2