a55146d869f0a02cb687fc9988a7ca375edc2430
[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       else
112       {
113         arguments.add(arg);
114       }
115     }
116
117     // use saved preferences if no cmdline args
118     boolean useCustomisedSettings = LaunchUtils
119             .getBooleanUserPreference(MemorySetting.CUSTOMISED_SETTINGS);
120     if (useCustomisedSettings)
121     {
122       if (jvmmempc == null)
123       {
124         jvmmempc = LaunchUtils
125                 .getUserPreference(MemorySetting.MEMORY_JVMMEMPC);
126       }
127       if (jvmmemmax == null)
128       {
129         jvmmemmax = LaunchUtils
130                 .getUserPreference(MemorySetting.MEMORY_JVMMEMMAX);
131       }
132     }
133
134     // add memory setting if not specified
135     boolean memSet = false;
136     boolean dockIcon = false;
137     boolean dockName = false;
138     for (int i = 0; i < command.size(); i++)
139     {
140       String arg = command.get(i);
141       if (arg.startsWith("-Xmx"))
142       {
143         // only use -Xmx if jvmmemmax and jvmmempc have not been set
144         if (jvmmempc == null && jvmmemmax == null)
145         {
146           memSetting = arg;
147           memSet = true;
148         }
149       }
150       else if (arg.startsWith("-Xdock:icon"))
151       {
152         dockIcon = true;
153       }
154       else if (arg.startsWith("-Xdock:name"))
155       {
156         dockName = true;
157       }
158     }
159
160     if (!memSet)
161     {
162       long maxMemLong = MemorySetting.getMemorySetting(jvmmemmax, jvmmempc);
163
164       if (maxMemLong > 0)
165       {
166         memSetting = "-Xmx" + Long.toString(maxMemLong);
167         memSet = true;
168         command.add(memSetting);
169       }
170     }
171
172     if (isAMac)
173     {
174       if (!dockIcon)
175       {
176         command.add("-Xdock:icon=" + dockIconPath);
177       }
178       if (!dockName)
179       {
180         // -Xdock:name=... doesn't actually work :(
181         // Leaving it in in case it gets fixed
182         command.add(
183                 "-Xdock:name=" + ChannelProperties.getProperty("app_name"));
184       }
185     }
186
187     String scalePropertyArg = HiDPISetting.getScalePropertyArg();
188     if (scalePropertyArg != null)
189     {
190       System.out.println("Running " + startClass + " with scale setting "
191               + scalePropertyArg);
192       command.add(scalePropertyArg);
193     }
194
195     command.add(startClass);
196     command.addAll(arguments);
197
198     final ProcessBuilder builder = new ProcessBuilder(command);
199
200     if (Boolean.parseBoolean(System.getProperty("launcherprint", "false")))
201     {
202       System.out.println(
203               "LAUNCHER COMMAND: " + String.join(" ", builder.command()));
204     }
205     System.out.println("Running " + startClass + " with "
206             + (memSetting == null ? "no memory setting"
207                     : ("memory setting " + memSetting)));
208
209     if (Boolean.parseBoolean(System.getProperty("launcherstop", "false")))
210     {
211       System.exit(0);
212     }
213     try
214     {
215       builder.inheritIO();
216       Process process = builder.start();
217       process.waitFor();
218     } catch (IOException e)
219     {
220       if (e.getMessage().toLowerCase(Locale.ROOT).contains("memory"))
221       {
222         System.out.println("Caught a memory exception: " + e.getMessage());
223         // Probably the "Cannot allocate memory" error, try without the memory
224         // setting
225         ArrayList<String> commandNoMem = new ArrayList<>();
226         for (int i = 0; i < command.size(); i++)
227         {
228           if (!command.get(i).startsWith("-Xmx"))
229           {
230             commandNoMem.add(command.get(i));
231           }
232         }
233         final ProcessBuilder builderNoMem = new ProcessBuilder(
234                 commandNoMem);
235         System.out.println("Command without memory setting: "
236                 + String.join(" ", builderNoMem.command()));
237         try
238         {
239           builderNoMem.inheritIO();
240           Process processNoMem = builderNoMem.start();
241           processNoMem.waitFor();
242         } catch (Exception ex)
243         {
244           ex.printStackTrace();
245         }
246       }
247       else
248       {
249         e.printStackTrace();
250       }
251     } catch (Exception e)
252     {
253       e.printStackTrace();
254     }
255     // System.exit(0);
256   }
257
258 }