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 headlessProperty = "java.awt.headless";
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.
63 public static void main(String[] args)
65 if (!LaunchUtils.checkJavaVersion())
68 .errPrintln("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 String jvmmempc = null;
75 String jvmmemmax = null;
76 boolean debug = false;
78 boolean quiet = false;
79 boolean headless = false;
81 boolean stdout = false;
82 // must set --debug before --launcher...
83 boolean launcherstop = false;
84 boolean launcherprint = false;
85 boolean launcherwait = false;
86 ArrayList<String> arguments = new ArrayList<>();
87 String previousArg = null;
88 for (String arg : args)
90 if (arg.equals("--debug"))
94 if (arg.equals("--quiet"))
98 if (arg.equals("--headless"))
102 if (arg.equals("--gui"))
106 if (arg.equals("--output=-")
107 || (arg.equals("-") && "--output".equals(previousArg)))
111 if (debug && arg.equals("--launcherprint"))
113 launcherprint = true;
115 if (debug && arg.equals("--launcherstop"))
119 if (debug && arg.equals("--launcherwait"))
123 // this ends the launcher immediately
124 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);
171 // --gui takes precedence over --headless
175 final String appName = ChannelProperties.getProperty("app_name");
177 // if we're using jalview.bin.Launcher we always assume a console is in use
178 final String javaBin = LaunchUtils.findJavaBin(true);
180 List<String> command = new ArrayList<>();
181 command.add(javaBin);
183 String memSetting = null;
185 for (String jvmArg : ManagementFactory.getRuntimeMXBean()
186 .getInputArguments())
191 command.add(ManagementFactory.getRuntimeMXBean().getClassPath());
193 // use saved preferences if no cmdline args
194 boolean useCustomisedSettings = LaunchUtils
195 .getBooleanUserPreference(MemorySetting.CUSTOMISED_SETTINGS);
196 if (useCustomisedSettings)
198 if (jvmmempc == null)
200 jvmmempc = LaunchUtils
201 .getUserPreference(MemorySetting.MEMORY_JVMMEMPC);
203 if (jvmmemmax == null)
205 jvmmemmax = LaunchUtils
206 .getUserPreference(MemorySetting.MEMORY_JVMMEMMAX);
210 // add these settings if not already specified
211 boolean memSet = false;
212 boolean dockIcon = false;
213 boolean dockName = false;
214 boolean headlessProp = false;
215 for (int i = 0; i < command.size(); i++)
217 String arg = command.get(i);
218 if (arg.startsWith("-Xmx"))
220 // only use -Xmx if jvmmemmax and jvmmempc have not been set
221 if (jvmmempc == null && jvmmemmax == null)
227 else if (arg.startsWith("-Xdock:icon"))
231 else if (arg.startsWith("-Xdock:name"))
235 else if (arg.startsWith("-D" + headlessProperty + "="))
243 long maxMemLong = MemorySetting.getMemorySetting(jvmmemmax, jvmmempc);
247 memSetting = "-Xmx" + Long.toString(maxMemLong);
249 command.add(memSetting);
253 if (LaunchUtils.isMac)
257 String dockIconPath = System.getProperty("getdownappdir", ".")
258 + File.separator + "resource/jalview_logo.png";
259 command.add("-Xdock:icon=" + dockIconPath);
263 // -Xdock:name=... doesn't actually work :(
264 // Leaving it in in case it gets fixed
265 command.add("-Xdock:name=" + appName);
266 // this launches WITHOUT an icon in the macOS dock. Could be useful for
268 // command.add("-Dapple.awt.UIElement=false");
269 // This also does not work for the dock
270 command.add("-Dcom.apple.mrj.application.apple.menu.about.name="
274 if (headless && !headlessProp)
276 System.setProperty(headlessProperty, "true");
277 command.add("-D" + headlessProperty + "=true");
280 String scalePropertyArg = HiDPISetting.getScalePropertyArg();
281 if (scalePropertyArg != null)
283 syserr(debug, quiet, "Running " + startClass + " with scale setting "
285 command.add(scalePropertyArg);
288 command.add(startClass);
289 command.addAll(arguments);
291 final ProcessBuilder builder = new ProcessBuilder(command);
293 if ((Boolean.parseBoolean(System.getProperty("launcherprint", "false"))
297 "LAUNCHER COMMAND: " + String.join(" ", builder.command()));
300 "Running " + startClass + " with "
301 + (memSetting == null ? "no memory setting"
302 : ("memory setting " + memSetting)));
304 if (Boolean.parseBoolean(System.getProperty("launcherstop", "false"))
305 || (debug && launcherstop))
308 "System property 'launcherstop' is set and not 'false'. Exiting.");
314 Process process = builder.start();
315 if (wait || launcherwait)
317 syserr(debug, quiet, "Launching application process");
324 "Wait time for application process is " + waitInt + "ms");
325 process.waitFor(waitInt, TimeUnit.MILLISECONDS);
327 syserr(debug, quiet, "Launcher process ending");
328 } catch (IOException e)
330 if (e.getMessage().toLowerCase(Locale.ROOT).contains("memory"))
333 .errPrintln("Caught a memory exception: " + e.getMessage());
334 // Probably the "Cannot allocate memory" error, try without the memory
336 ArrayList<String> commandNoMem = new ArrayList<>();
337 for (int i = 0; i < command.size(); i++)
339 if (!command.get(i).startsWith("-Xmx"))
341 commandNoMem.add(command.get(i));
344 final ProcessBuilder builderNoMem = new ProcessBuilder(
346 jalview.bin.Console.errPrintln("Command without memory setting: "
347 + String.join(" ", builderNoMem.command()));
350 builderNoMem.inheritIO();
351 Process processNoMem = builderNoMem.start();
352 processNoMem.waitFor();
353 } catch (Exception ex)
355 ex.printStackTrace();
362 } catch (Exception e)
368 private static void syserr(boolean debug, boolean quiet, String message)
372 jalview.bin.Console.errPrintln("LAUNCHERDEBUG - " + message);