JAL-3449 install4j version, install4j template version, getdown version now all being...
[jalview.git] / src / jalview / bin / Launcher.java
index aec3acd..041a7b1 100644 (file)
@@ -1,17 +1,14 @@
 package jalview.bin;
 
 import java.io.File;
+import java.io.IOException;
 import java.lang.management.ManagementFactory;
-import java.lang.management.OperatingSystemMXBean;
 import java.util.ArrayList;
 
 public class Launcher
 {
-
   private final static String startClass = "jalview.bin.Jalview";
 
-  private final static int maxHeapSizePerCent = 95;
-
   private final static String dockIconPath = "JalviewLogo_Huge.png";
 
   public static void main(String[] args)
@@ -22,6 +19,8 @@ public class Launcher
     ArrayList<String> command = new ArrayList<>();
     command.add(javaBin);
 
+    String memSetting = null;
+
     boolean isAMac = System.getProperty("os.name").indexOf("Mac") > -1;
 
     for (String jvmArg : ManagementFactory.getRuntimeMXBean()
@@ -40,39 +39,48 @@ public class Launcher
     // add memory setting if not specified
     boolean memSet = false;
     boolean dockIcon = false;
+    boolean dockName = false;
     ARG: for (int i = 0; i < command.size(); i++)
     {
       String arg = command.get(i);
       if (arg.startsWith("-Xmx"))
       {
+        memSetting = arg;
         memSet = true;
       }
       else if (arg.startsWith("-Xdock:icon"))
       {
         dockIcon = true;
       }
+      else if (arg.startsWith("-Xdock:name"))
+      {
+        dockName = true;
+      }
     }
 
     if (!memSet)
     {
-      long maxMemLong = -1;
-      long physicalMem = getPhysicalMemory();
-      if (physicalMem > 0)
-      {
-        maxMemLong = physicalMem * maxHeapSizePerCent / 100;
-      }
+      long maxMemLong = MemorySetting.getMemorySetting();
+      
       if (maxMemLong > 0)
       {
-        command.add("-Xmx" + Long.toString(maxMemLong));
+        memSetting = "-Xmx" + Long.toString(maxMemLong);
+        command.add(memSetting);
       }
     }
 
-    if (!dockIcon && isAMac)
+    if (isAMac)
     {
-      command.add("-Xdock:icon=" + dockIconPath);
-      // -Xdock:name=... doesn't actually work :(
-      // Leaving it in in case it gets fixed
-      command.add("-Xdock:name=" + "Jalview");
+      if (!dockIcon)
+      {
+        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=" + "Jalview");
+      }
     }
 
     command.add(startClass);
@@ -80,41 +88,58 @@ public class Launcher
 
     final ProcessBuilder builder = new ProcessBuilder(command);
 
-    System.out.println("COMMAND: " + String.join(" ", builder.command()));
+    // System.out.println("COMMAND: " + String.join(" ", builder.command()));
+    System.out.println("Running " + startClass + " with "
+            + (memSetting == null ? "no memSetting" : memSetting));
 
+    if (System.getProperty("launcherstop") != null
+            && System.getProperty("launcherstop").equals("true"))
+    {
+      System.exit(0);
+    }
     try
     {
       builder.inheritIO();
       Process process = builder.start();
       process.waitFor();
-    } catch (Exception e)
+    } catch (IOException e)
     {
-      e.printStackTrace();
-    }
-    // System.exit(0);
-
-  }
-
-  public static long getPhysicalMemory()
-  {
-    final OperatingSystemMXBean o = ManagementFactory
-            .getOperatingSystemMXBean();
-
-    try
-    {
-      if (o instanceof com.sun.management.OperatingSystemMXBean)
+      if (e.getMessage().toLowerCase().contains("memory"))
+      {
+        System.out.println("Caught a memory exception: " + e.getMessage());
+        // Probably the "Cannot allocate memory" error, try without the memory setting
+        ArrayList<String> commandNoMem = new ArrayList<>();
+        for (int i = 0; i < command.size(); i++)
+        {
+          if (!command.get(i).startsWith("-Xmx"))
+          {
+            commandNoMem.add(command.get(i));
+          }
+        }
+        final ProcessBuilder builderNoMem = new ProcessBuilder(
+                commandNoMem);
+        System.out.println("NO MEM COMMAND: "
+                + String.join(" ", builderNoMem.command()));
+        try
+        {
+          builderNoMem.inheritIO();
+          Process processNoMem = builderNoMem.start();
+          processNoMem.waitFor();
+        } catch (Exception ex)
+        {
+          ex.printStackTrace();
+        }
+      }
+      else
       {
-        final com.sun.management.OperatingSystemMXBean osb = (com.sun.management.OperatingSystemMXBean) o;
-        return osb.getTotalPhysicalMemorySize();
+        e.printStackTrace();
       }
-    } catch (NoClassDefFoundError e)
+    } catch (Exception e)
     {
-      // com.sun.management.OperatingSystemMXBean doesn't exist in this JVM
-      System.out.println("No com.sun.management.OperatingSystemMXBean");
+      e.printStackTrace();
     }
+    // System.exit(0);
 
-    // We didn't get a com.sun.management.OperatingSystemMXBean.
-    return -1;
   }
 
 }