JAL-3830 Look for --headless mode and add -Djava.awt.headless=true to java invocation...
[jalview.git] / src / jalview / bin / Launcher.java
index f29e0a1..f199002 100644 (file)
@@ -50,8 +50,30 @@ public class Launcher
 {
   private final static String startClass = "jalview.bin.Jalview";
 
-  private final static String dockIconPath = ChannelProperties
-          .getProperty("logo.512");
+  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
@@ -70,9 +92,22 @@ public class Launcher
               + ") 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";
+    final String appName = ChannelProperties.getProperty("app_name");
+    final String javaBinDir = System.getProperty("java.home")
+            + File.separator + "bin" + File.separator;
+    String javaBin = null;
+    if (javaBin == null && checkJVMSymlink(javaBinDir + appName))
+    {
+      javaBin = javaBinDir + appName;
+    }
+    if (javaBin == null && checkJVMSymlink(javaBinDir + "Jalview"))
+    {
+      javaBin = javaBinDir + "Jalview";
+    }
+    if (javaBin == null)
+    {
+      javaBin = "java";
+    }
 
     List<String> command = new ArrayList<>();
     command.add(javaBin);
@@ -94,6 +129,7 @@ public class Launcher
     boolean debug = false;
     boolean wait = true;
     boolean quiet = false;
+    boolean headless = false;
     // must set --debug before --launcher...
     boolean launcherstop = false;
     boolean launcherprint = false;
@@ -109,6 +145,10 @@ public class Launcher
       {
         quiet = true;
       }
+      if (arg.equals("--headless"))
+      {
+        headless = true;
+      }
       if (debug && arg.equals("--launcherprint"))
       {
         launcherprint = true;
@@ -188,6 +228,7 @@ public class Launcher
     boolean memSet = false;
     boolean dockIcon = false;
     boolean dockName = false;
+    boolean headlessProp = false;
     for (int i = 0; i < command.size(); i++)
     {
       String arg = command.get(i);
@@ -208,6 +249,10 @@ public class Launcher
       {
         dockName = true;
       }
+      else if (arg.startsWith("-D" + headlessProperty + "="))
+      {
+        headlessProp = true;
+      }
     }
 
     if (!memSet)
@@ -226,22 +271,28 @@ public class Launcher
     {
       if (!dockIcon)
       {
+        String dockIconPath = System.getProperty("getdownappdir", ".")
+                + File.separator + "resource/jalview_logo.png";
         command.add("-Xdock:icon=" + dockIconPath);
       }
       if (!dockName)
       {
         // -Xdock:name=... doesn't actually work :(
         // Leaving it in in case it gets fixed
-        command.add(
-                "-Xdock:name=" + ChannelProperties.getProperty("app_name"));
+        command.add("-Xdock:name=" + appName);
         // this launches WITHOUT an icon in the macOS dock. Could be useful for
         // getdown?
         // command.add("-Dapple.awt.UIElement=false");
         // This also does not work for the dock
         command.add("-Dcom.apple.mrj.application.apple.menu.about.name="
-                + ChannelProperties.getProperty("app_name"));
+                + appName);
       }
     }
+    if (headless && !headlessProp)
+    {
+      System.setProperty(headlessProperty, "true");
+      command.add("-D" + headlessProperty + "=true");
+    }
 
     String scalePropertyArg = HiDPISetting.getScalePropertyArg();
     if (scalePropertyArg != null)