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