2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
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 import java.util.concurrent.TimeUnit;
31 import jalview.util.ChannelProperties;
32 import jalview.util.LaunchUtils;
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.
51 private final static String startClass = "jalview.bin.Jalview";
53 private final static String dockIconPath = ChannelProperties
54 .getProperty("logo.512");
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.
64 public static void main(String[] args)
66 if (!LaunchUtils.checkJavaVersion())
68 System.err.println("WARNING - The Java version being used (Java "
69 + LaunchUtils.getJavaVersion()
70 + ") may lead to problems. This installation of Jalview should be used with Java "
71 + LaunchUtils.getJavaCompileVersion() + ".");
74 final String javaBin = System.getProperty("java.home") + File.separator
75 + "bin" + File.separator + "java";
77 List<String> command = new ArrayList<>();
80 String memSetting = null;
82 boolean isAMac = System.getProperty("os.name").indexOf("Mac") > -1;
84 for (String jvmArg : ManagementFactory.getRuntimeMXBean()
90 command.add(ManagementFactory.getRuntimeMXBean().getClassPath());
92 String jvmmempc = null;
93 String jvmmemmax = null;
94 boolean debug = false;
96 boolean quiet = false;
97 // must set --debug before --launcher...
98 boolean launcherstop = false;
99 boolean launcherprint = false;
100 boolean launcherwait = false;
101 ArrayList<String> arguments = new ArrayList<>();
102 for (String arg : args)
104 if (arg.equals("--debug"))
108 if (arg.equals("--quiet"))
112 if (debug && arg.equals("--launcherprint"))
114 launcherprint = true;
116 if (debug && arg.equals("--launcherstop"))
120 if (debug && arg.equals("--launcherwait"))
124 // this ends the launcher immediately
125 if (debug && arg.equals("--launchernowait"))
129 // Don't add the --launcher... args to Jalview launch
130 if (arg.startsWith("--launcher"))
134 // jvmmempc and jvmmemmax args used to set memory and are not passed on to
137 "-" + MemorySetting.MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "="))
139 jvmmempc = arg.substring(
140 MemorySetting.MAX_HEAPSIZE_PERCENT_PROPERTY_NAME.length()
143 else if (arg.startsWith(
144 "-" + MemorySetting.MAX_HEAPSIZE_PROPERTY_NAME + "="))
146 jvmmemmax = arg.substring(
147 MemorySetting.MAX_HEAPSIZE_PROPERTY_NAME.length() + 2);
149 // --doubledash versions
150 else if (arg.startsWith("--"
151 + MemorySetting.MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "="))
153 jvmmempc = arg.substring(
154 MemorySetting.MAX_HEAPSIZE_PERCENT_PROPERTY_NAME.length()
157 else if (arg.startsWith(
158 "--" + MemorySetting.MAX_HEAPSIZE_PROPERTY_NAME + "="))
160 jvmmemmax = arg.substring(
161 MemorySetting.MAX_HEAPSIZE_PROPERTY_NAME.length() + 3);
170 // use saved preferences if no cmdline args
171 boolean useCustomisedSettings = LaunchUtils
172 .getBooleanUserPreference(MemorySetting.CUSTOMISED_SETTINGS);
173 if (useCustomisedSettings)
175 if (jvmmempc == null)
177 jvmmempc = LaunchUtils
178 .getUserPreference(MemorySetting.MEMORY_JVMMEMPC);
180 if (jvmmemmax == null)
182 jvmmemmax = LaunchUtils
183 .getUserPreference(MemorySetting.MEMORY_JVMMEMMAX);
187 // add these settings if not already specified
188 boolean memSet = false;
189 boolean dockIcon = false;
190 boolean dockName = false;
191 for (int i = 0; i < command.size(); i++)
193 String arg = command.get(i);
194 if (arg.startsWith("-Xmx"))
196 // only use -Xmx if jvmmemmax and jvmmempc have not been set
197 if (jvmmempc == null && jvmmemmax == null)
203 else if (arg.startsWith("-Xdock:icon"))
207 else if (arg.startsWith("-Xdock:name"))
215 long maxMemLong = MemorySetting.getMemorySetting(jvmmemmax, jvmmempc);
219 memSetting = "-Xmx" + Long.toString(maxMemLong);
221 command.add(memSetting);
229 command.add("-Xdock:icon=" + dockIconPath);
233 // -Xdock:name=... doesn't actually work :(
234 // Leaving it in in case it gets fixed
236 "-Xdock:name=" + ChannelProperties.getProperty("app_name"));
237 // this launches WITHOUT an icon in the macOS dock. Could be useful for
239 // command.add("-Dapple.awt.UIElement=false");
240 // This also does not work for the dock
241 command.add("-Dcom.apple.mrj.application.apple.menu.about.name="
242 + ChannelProperties.getProperty("app_name"));
246 String scalePropertyArg = HiDPISetting.getScalePropertyArg();
247 if (scalePropertyArg != null)
249 sysout(debug, quiet, "Running " + startClass + " with scale setting "
251 command.add(scalePropertyArg);
254 command.add(startClass);
255 command.addAll(arguments);
257 final ProcessBuilder builder = new ProcessBuilder(command);
259 if ((Boolean.parseBoolean(System.getProperty("launcherprint", "false"))
263 "LAUNCHER COMMAND: " + String.join(" ", builder.command()));
266 "Running " + startClass + " with "
267 + (memSetting == null ? "no memory setting"
268 : ("memory setting " + memSetting)));
270 if (Boolean.parseBoolean(System.getProperty("launcherstop", "false"))
271 || (debug && launcherstop))
274 "System property 'launcherstop' is set and not 'false'. Exiting.");
280 Process process = builder.start();
281 if (wait || launcherwait)
283 sysout(debug, quiet, "Launching application process");
290 "Wait time for application process is " + waitInt + "ms");
291 process.waitFor(waitInt, TimeUnit.MILLISECONDS);
293 sysout(debug, quiet, "Launcher process ending");
294 } catch (IOException e)
296 if (e.getMessage().toLowerCase(Locale.ROOT).contains("memory"))
298 System.err.println("Caught a memory exception: " + e.getMessage());
299 // Probably the "Cannot allocate memory" error, try without the memory
301 ArrayList<String> commandNoMem = new ArrayList<>();
302 for (int i = 0; i < command.size(); i++)
304 if (!command.get(i).startsWith("-Xmx"))
306 commandNoMem.add(command.get(i));
309 final ProcessBuilder builderNoMem = new ProcessBuilder(
311 System.err.println("Command without memory setting: "
312 + String.join(" ", builderNoMem.command()));
315 builderNoMem.inheritIO();
316 Process processNoMem = builderNoMem.start();
317 processNoMem.waitFor();
318 } catch (Exception ex)
320 ex.printStackTrace();
327 } catch (Exception e)
333 private static void sysout(boolean debug, boolean quiet, String message)
337 System.out.println("LAUNCHERDEBUG - " + message);