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