From ed379d3f998a6aea83ce8c9cdaab2ec8234adb6a Mon Sep 17 00:00:00 2001 From: Ben Soares Date: Wed, 16 Aug 2023 17:45:56 +0100 Subject: [PATCH] JAL-3820 JAL-4189 Move checks for java executable symlinks to LaunchUtils ready for use in getdown --- src/jalview/bin/HiDPISetting.java | 16 ++++-- src/jalview/bin/Launcher.java | 75 +++++++--------------------- src/jalview/util/LaunchUtils.java | 100 +++++++++++++++++++++++++++++++++++-- 3 files changed, 127 insertions(+), 64 deletions(-) diff --git a/src/jalview/bin/HiDPISetting.java b/src/jalview/bin/HiDPISetting.java index 2bce673..77228c0 100644 --- a/src/jalview/bin/HiDPISetting.java +++ b/src/jalview/bin/HiDPISetting.java @@ -20,9 +20,8 @@ */ package jalview.bin; -import java.util.Locale; - import java.awt.HeadlessException; +import java.util.Locale; public class HiDPISetting { @@ -159,7 +158,11 @@ public class HiDPISetting dpi = screenInfo.getScreenResolution(); } catch (HeadlessException e) { - System.err.println("Cannot get screen resolution: " + e.getMessage()); + if (isLinux) + { + System.err + .println("Cannot get screen resolution: " + e.getMessage()); + } } // try and get screen size height and width @@ -171,8 +174,11 @@ public class HiDPISetting mindimension = Math.min(height, width); } catch (HeadlessException e) { - System.err.println( - "Cannot get screen size height and width:" + e.getMessage()); + if (isLinux) + { + System.err.println("Cannot get screen size height and width:" + + e.getMessage()); + } } // attempt at a formula for scaling based on screen dpi and mindimension. diff --git a/src/jalview/bin/Launcher.java b/src/jalview/bin/Launcher.java index c333e2e..20a466b 100644 --- a/src/jalview/bin/Launcher.java +++ b/src/jalview/bin/Launcher.java @@ -52,29 +52,6 @@ public class Launcher private final static String headlessProperty = "java.awt.headless"; - private static boolean checkJVMSymlink(String testBin) - { - File testBinFile = new File(testBin); - if (!testBinFile.exists()) - { - return false; - } - File targetFile = null; - try - { - targetFile = testBinFile.getCanonicalFile(); - } catch (IOException e) - { - return false; - } - if (targetFile != null && ("java".equals(targetFile.getName()) - || "java.exe".equals(targetFile.getName()))) - { - return true; - } - return false; - } - /** * main method for jalview.bin.Launcher. This restarts the same JRE's JVM with * the same arguments but with memory adjusted based on extracted -jvmmempc @@ -92,38 +69,6 @@ public class Launcher + ") may lead to problems. This installation of Jalview should be used with Java " + LaunchUtils.getJavaCompileVersion() + "."); } - final String appName = ChannelProperties.getProperty("app_name"); - final String javaBinDir = System.getProperty("java.home") - + File.separator + "bin" + File.separator; - String javaBin = null; - for (String name : new String[] { appName, "Jalview", "java", - "java.exe" }) - { - if (checkJVMSymlink(javaBinDir + name)) - { - javaBin = javaBinDir + name; - break; - } - } - if (javaBin == null) - { - javaBin = "java"; - } - - List command = new ArrayList<>(); - command.add(javaBin); - - String memSetting = null; - - boolean isAMac = System.getProperty("os.name").indexOf("Mac") > -1; - - for (String jvmArg : ManagementFactory.getRuntimeMXBean() - .getInputArguments()) - { - command.add(jvmArg); - } - command.add("-cp"); - command.add(ManagementFactory.getRuntimeMXBean().getClassPath()); String jvmmempc = null; String jvmmemmax = null; @@ -218,6 +163,24 @@ public class Launcher headless = false; } + final String appName = ChannelProperties.getProperty("app_name"); + + // if we're using jalview.bin.Launcher we always assume a console is in use + final String javaBin = LaunchUtils.findJavaBin(true); + + List command = new ArrayList<>(); + command.add(javaBin); + + String memSetting = null; + + for (String jvmArg : ManagementFactory.getRuntimeMXBean() + .getInputArguments()) + { + command.add(jvmArg); + } + command.add("-cp"); + command.add(ManagementFactory.getRuntimeMXBean().getClassPath()); + // use saved preferences if no cmdline args boolean useCustomisedSettings = LaunchUtils .getBooleanUserPreference(MemorySetting.CUSTOMISED_SETTINGS); @@ -278,7 +241,7 @@ public class Launcher } } - if (isAMac) + if (LaunchUtils.isMac) { if (!dockIcon) { diff --git a/src/jalview/util/LaunchUtils.java b/src/jalview/util/LaunchUtils.java index 5bd4a08..784eb5a 100644 --- a/src/jalview/util/LaunchUtils.java +++ b/src/jalview/util/LaunchUtils.java @@ -32,6 +32,16 @@ import java.util.Properties; public class LaunchUtils { + // setting these is LaunchUtils so don't need to import Platform + public final static boolean isMac = System.getProperty("os.name") + .indexOf("Mac") > -1; + + public final static boolean isWindows = System.getProperty("os.name") + .indexOf("Win") > -1; + + private static boolean isJS = /** @j2sNative true || */ + false; + public static void loadChannelProps(File dir) { ChannelProperties.loadProps(dir); @@ -81,7 +91,7 @@ public class LaunchUtils public static int getJavaCompileVersion() { - if (Platform.isJS()) + if (LaunchUtils.isJS) { return -1; } @@ -129,7 +139,7 @@ public class LaunchUtils public static int getJavaVersion() { - if (Platform.isJS()) + if (LaunchUtils.isJS) { return -1; } @@ -161,7 +171,7 @@ public class LaunchUtils public static boolean checkJavaVersion() { - if (Platform.isJS()) + if (LaunchUtils.isJS) { return true; } @@ -186,4 +196,88 @@ public class LaunchUtils return true; } + + public static String findJavaBin(boolean winConsole) + { + return findJavaBin(System.getProperty("java.home"), winConsole, true); + } + + /* + * Returns a string path to the most likely java binary wanted to run this + * installation of Jalview. + * + * @param winConsole whether to use java.exe (console) in preference to javaw.exe + * (only affects Windows). + * @param javaHome Try this javaHome dir (defaults to the running java.home). + * @param generic Return a generic java command if not found. + */ + public static String findJavaBin(String javaHome, boolean winConsole, + boolean generic) + { + String javaBin = null; + final String javaExe = winConsole ? "java.exe" : "javaw.exe"; + final String java = "java"; + + if (javaHome != null) + { + // property "channel.app_name" is set by install4j when launching getdown + String propertyAppName = System.getProperty("channel.app_name"); + final String appName = (propertyAppName != null + && propertyAppName.length() > 0) ? propertyAppName + : ChannelProperties.getProperty("app_name"); + + final String javaBinDir = javaHome + File.separator + "bin" + + File.separator; + + // appName and "Jalview" will not point to javaw.exe or java.exe but in + // this case that's okay because the taskbar display name problem doesn't + // manifest in Windows. See JAL-3820, JAL-4189. + for (String name : new String[] { appName, "Jalview", java, javaExe }) + { + if (LaunchUtils.checkJVMSymlink(javaBinDir + name, winConsole)) + { + javaBin = javaBinDir + name; + break; + } + } + } + + if (javaBin == null && generic) + { + javaBin = LaunchUtils.isWindows ? javaExe : java; + } + + return javaBin; + } + + /* + * checkJVMSymlink returns true if the path in testBin *is* a java binary, or + * points to a java binary. + * @param testBin The binary or symbolic link to check + * @param winConsole whether we are in/want a Windows console (only relevant for Windows, + * determines whether we use java.exe or javaw.exe) + */ + private static boolean checkJVMSymlink(String testBin, boolean winConsole) + { + File testBinFile = new File(testBin); + if (!testBinFile.exists()) + { + return false; + } + File targetFile = null; + try + { + targetFile = testBinFile.getCanonicalFile(); + } catch (IOException e) + { + return false; + } + final String javaExe = winConsole ? "java.exe" : "javaw.exe"; + if (targetFile != null && ("java".equals(targetFile.getName()) + || javaExe.equals(targetFile.getName()))) + { + return true; + } + return false; + } } -- 1.7.10.2