JAL-3407 release notes for JAL-3449 JAL-3393 JAL-3420 JAL-3477 JAL-3447 JAL-3542
[jalview.git] / src / jalview / bin / Launcher.java
1 package jalview.bin;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.lang.management.ManagementFactory;
6 import java.util.ArrayList;
7
8 public class Launcher
9 {
10   private final static String startClass = "jalview.bin.Jalview";
11
12   private final static String dockIconPath = "JalviewLogo_Huge.png";
13
14   public static void main(String[] args)
15   {
16     final String javaBin = System.getProperty("java.home") + File.separator
17             + "bin" + File.separator + "java";
18
19     ArrayList<String> command = new ArrayList<>();
20     command.add(javaBin);
21
22     String memSetting = null;
23
24     boolean isAMac = System.getProperty("os.name").indexOf("Mac") > -1;
25
26     for (String jvmArg : ManagementFactory.getRuntimeMXBean()
27             .getInputArguments())
28     {
29       command.add(jvmArg);
30     }
31     command.add("-cp");
32     command.add(ManagementFactory.getRuntimeMXBean().getClassPath());
33     ArrayList<String> arguments = new ArrayList<>();
34     for (String arg : args)
35     {
36       arguments.add(arg);
37     }
38
39     // add memory setting if not specified
40     boolean memSet = false;
41     boolean dockIcon = false;
42     boolean dockName = false;
43     ARG: for (int i = 0; i < command.size(); i++)
44     {
45       String arg = command.get(i);
46       if (arg.startsWith("-Xmx"))
47       {
48         memSetting = arg;
49         memSet = true;
50       }
51       else if (arg.startsWith("-Xdock:icon"))
52       {
53         dockIcon = true;
54       }
55       else if (arg.startsWith("-Xdock:name"))
56       {
57         dockName = true;
58       }
59     }
60
61     if (!memSet)
62     {
63       long maxMemLong = MemorySetting.getMemorySetting();
64       
65       if (maxMemLong > 0)
66       {
67         memSetting = "-Xmx" + Long.toString(maxMemLong);
68         command.add(memSetting);
69       }
70     }
71
72     if (isAMac)
73     {
74       if (!dockIcon)
75       {
76         command.add("-Xdock:icon=" + dockIconPath);
77       }
78       if (!dockName)
79       {
80         // -Xdock:name=... doesn't actually work :(
81         // Leaving it in in case it gets fixed
82         command.add("-Xdock:name=" + "Jalview");
83       }
84     }
85
86     command.add(startClass);
87     command.addAll(arguments);
88
89     final ProcessBuilder builder = new ProcessBuilder(command);
90
91     // System.out.println("COMMAND: " + String.join(" ", builder.command()));
92     System.out.println("Running " + startClass + " with "
93             + (memSetting == null ? "no memSetting" : memSetting));
94
95     if (System.getProperty("launcherstop") != null
96             && System.getProperty("launcherstop").equals("true"))
97     {
98       System.exit(0);
99     }
100     try
101     {
102       builder.inheritIO();
103       Process process = builder.start();
104       process.waitFor();
105     } catch (IOException e)
106     {
107       if (e.getMessage().toLowerCase().contains("memory"))
108       {
109         System.out.println("Caught a memory exception: " + e.getMessage());
110         // Probably the "Cannot allocate memory" error, try without the memory setting
111         ArrayList<String> commandNoMem = new ArrayList<>();
112         for (int i = 0; i < command.size(); i++)
113         {
114           if (!command.get(i).startsWith("-Xmx"))
115           {
116             commandNoMem.add(command.get(i));
117           }
118         }
119         final ProcessBuilder builderNoMem = new ProcessBuilder(
120                 commandNoMem);
121         System.out.println("NO MEM COMMAND: "
122                 + String.join(" ", builderNoMem.command()));
123         try
124         {
125           builderNoMem.inheritIO();
126           Process processNoMem = builderNoMem.start();
127           processNoMem.waitFor();
128         } catch (Exception ex)
129         {
130           ex.printStackTrace();
131         }
132       }
133       else
134       {
135         e.printStackTrace();
136       }
137     } catch (Exception e)
138     {
139       e.printStackTrace();
140     }
141     // System.exit(0);
142
143   }
144
145 }