1ea17d69d7e7ab29ba627492f13a18c54ff40be0
[jalview.git] / src / jalview / bin / Launcher.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.bin;
22
23 import java.util.Locale;
24
25 import java.io.File;
26 import java.io.IOException;
27 import java.lang.management.ManagementFactory;
28 import java.util.ArrayList;
29 import java.util.List;
30
31 import jalview.util.ChannelProperties;
32 import jalview.util.LaunchUtils;
33
34 /**
35  * A Launcher class for Jalview. This class is used to launch Jalview from the
36  * shadowJar when Getdown is not used or available. It attempts to take all the
37  * command line arguments to pass on to the jalview.bin.Jalview class, but to
38  * insert a -Xmx memory setting to a sensible default, using the -jvmmempc and
39  * -jvmmemmax application arguments if specified. If not specified then system
40  * properties will be looked for by jalview.bin.MemorySetting. If the user has
41  * provided the JVM with a -Xmx setting directly and not set -jvmmempc or
42  * -jvmmemmax then this setting will be used and system properties ignored. If
43  * -Xmx is set as well as -jvmmempc or -jvmmemmax as argument(s) then the -Xmx
44  * argument will NOT be passed on to the main application launch.
45  * 
46  * @author bsoares
47  *
48  */
49 public class Launcher
50 {
51   private final static String startClass = "jalview.bin.Jalview";
52
53   private final static String dockIconPath = ChannelProperties
54           .getProperty("logo.512");
55
56   /**
57    * main method for jalview.bin.Launcher. This restarts the same JRE's JVM with
58    * the same arguments but with memory adjusted based on extracted -jvmmempc
59    * and -jvmmemmax application arguments. If on a Mac then extra dock:icon and
60    * dock:name arguments are also set.
61    * 
62    * @param args
63    */
64   public static void main(String[] args)
65   {
66     final String javaBin = System.getProperty("java.home") + File.separator
67             + "bin" + File.separator + "java";
68
69     List<String> command = new ArrayList<>();
70     command.add(javaBin);
71
72     String memSetting = null;
73
74     boolean isAMac = System.getProperty("os.name").indexOf("Mac") > -1;
75
76     for (String jvmArg : ManagementFactory.getRuntimeMXBean()
77             .getInputArguments())
78     {
79       command.add(jvmArg);
80     }
81     command.add("-cp");
82     command.add(ManagementFactory.getRuntimeMXBean().getClassPath());
83
84     String jvmmempc = null;
85     String jvmmemmax = null;
86     ArrayList<String> arguments = new ArrayList<>();
87     for (String arg : args)
88     {
89       // jvmmempc and jvmmemmax args used to set memory and are not passed on to
90       // startClass
91       if (arg.startsWith(
92               "-" + MemorySetting.MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "="))
93       {
94         jvmmempc = arg.substring(
95                 MemorySetting.MAX_HEAPSIZE_PERCENT_PROPERTY_NAME.length()
96                         + 2);
97       }
98       else if (arg.startsWith(
99               "-" + MemorySetting.MAX_HEAPSIZE_PROPERTY_NAME + "="))
100       {
101         jvmmemmax = arg.substring(
102                 MemorySetting.MAX_HEAPSIZE_PROPERTY_NAME.length() + 2);
103       }
104       else
105       {
106         arguments.add(arg);
107       }
108     }
109
110     // use saved preferences if no cmdline args
111     boolean useCustomisedSettings = LaunchUtils
112             .getBooleanUserPreference(MemorySetting.CUSTOMISED_SETTINGS);
113     if (useCustomisedSettings)
114     {
115       if (jvmmempc == null)
116       {
117         jvmmempc = LaunchUtils
118                 .getUserPreference(MemorySetting.MEMORY_JVMMEMPC);
119       }
120       if (jvmmemmax == null)
121       {
122         jvmmemmax = LaunchUtils
123                 .getUserPreference(MemorySetting.MEMORY_JVMMEMMAX);
124       }
125     }
126
127     // add memory setting if not specified
128     boolean memSet = false;
129     boolean dockIcon = false;
130     boolean dockName = false;
131     for (int i = 0; i < command.size(); i++)
132     {
133       String arg = command.get(i);
134       if (arg.startsWith("-Xmx"))
135       {
136         // only use -Xmx if jvmmemmax and jvmmempc have not been set
137         if (jvmmempc == null && jvmmemmax == null)
138         {
139           memSetting = arg;
140           memSet = true;
141         }
142       }
143       else if (arg.startsWith("-Xdock:icon"))
144       {
145         dockIcon = true;
146       }
147       else if (arg.startsWith("-Xdock:name"))
148       {
149         dockName = true;
150       }
151     }
152
153     if (!memSet)
154     {
155       long maxMemLong = MemorySetting.getMemorySetting(jvmmemmax, jvmmempc);
156
157       if (maxMemLong > 0)
158       {
159         memSetting = "-Xmx" + Long.toString(maxMemLong);
160         memSet = true;
161         command.add(memSetting);
162       }
163     }
164
165     if (isAMac)
166     {
167       if (!dockIcon)
168       {
169         command.add("-Xdock:icon=" + dockIconPath);
170       }
171       if (!dockName)
172       {
173         // -Xdock:name=... doesn't actually work :(
174         // Leaving it in in case it gets fixed
175         command.add(
176                 "-Xdock:name=" + ChannelProperties.getProperty("app_name"));
177       }
178     }
179
180     String scalePropertyArg = HiDPISetting.getScalePropertyArg();
181     if (scalePropertyArg != null)
182     {
183       System.out.println("Running " + startClass + " with scale setting "
184               + scalePropertyArg);
185       command.add(scalePropertyArg);
186     }
187
188     command.add(startClass);
189     command.addAll(arguments);
190
191     final ProcessBuilder builder = new ProcessBuilder(command);
192
193     if (Boolean.parseBoolean(System.getProperty("launcherprint", "false")))
194     {
195       System.out.println(
196               "LAUNCHER COMMAND: " + String.join(" ", builder.command()));
197     }
198     System.out.println("Running " + startClass + " with "
199             + (memSetting == null ? "no memory setting"
200                     : ("memory setting " + memSetting)));
201
202     if (Boolean.parseBoolean(System.getProperty("launcherstop", "false")))
203     {
204       System.exit(0);
205     }
206     try
207     {
208       builder.inheritIO();
209       Process process = builder.start();
210       process.waitFor();
211     } catch (IOException e)
212     {
213       if (e.getMessage().toLowerCase(Locale.ROOT).contains("memory"))
214       {
215         System.out.println("Caught a memory exception: " + e.getMessage());
216         // Probably the "Cannot allocate memory" error, try without the memory
217         // setting
218         ArrayList<String> commandNoMem = new ArrayList<>();
219         for (int i = 0; i < command.size(); i++)
220         {
221           if (!command.get(i).startsWith("-Xmx"))
222           {
223             commandNoMem.add(command.get(i));
224           }
225         }
226         final ProcessBuilder builderNoMem = new ProcessBuilder(
227                 commandNoMem);
228         System.out.println("Command without memory setting: "
229                 + String.join(" ", builderNoMem.command()));
230         try
231         {
232           builderNoMem.inheritIO();
233           Process processNoMem = builderNoMem.start();
234           processNoMem.waitFor();
235         } catch (Exception ex)
236         {
237           ex.printStackTrace();
238         }
239       }
240       else
241       {
242         e.printStackTrace();
243       }
244     } catch (Exception e)
245     {
246       e.printStackTrace();
247     }
248     // System.exit(0);
249   }
250
251 }