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.bin.argparser.Arg;
32 import jalview.util.ChannelProperties;
33 import jalview.util.LaunchUtils;
36 * A Launcher class for Jalview. This class is used to launch Jalview from the
37 * shadowJar when Getdown is not used or available. It attempts to take all the
38 * command line arguments to pass on to the jalview.bin.Jalview class, but to
39 * insert a -Xmx memory setting to a sensible default, using the --jvmmempc and
40 * -jvmmemmax application arguments if specified. If not specified then system
41 * properties will be looked for by jalview.bin.MemorySetting. If the user has
42 * provided the JVM with a -Xmx setting directly and not set --jvmmempc or
43 * --jvmmemmax then this setting will be used and system properties ignored. If
44 * -Xmx is set as well as --jvmmempc or --jvmmemmax as argument(s) then the -Xmx
45 * argument will NOT be passed on to the main application launch.
52 private final static String startClass = "jalview.bin.Jalview";
54 // not setting this yet due to problem with Jmol
55 private final static String headlessProperty = "java.awt.headless";
57 // used for headless macOS
58 private final static String macosHeadlessProperty = "apple.awt.UIElement";
60 // arguments that assume headless mode
61 private final static String[] assumeHeadlessArgs = { "headless", "output",
62 "image", "structureimage" };
65 * main method for jalview.bin.Launcher. This restarts the same JRE's JVM with
66 * the same arguments but with memory adjusted based on extracted --jvmmempc
67 * and --jvmmemmax application arguments. If on a Mac then extra dock:icon and
68 * dock:name arguments are also set.
72 public static void main(String[] args)
74 if (!LaunchUtils.checkJavaVersion())
77 .errPrintln("WARNING - The Java version being used (Java "
78 + LaunchUtils.getJavaVersion()
79 + ") may lead to problems. This installation of Jalview should be used with Java "
80 + LaunchUtils.getJavaCompileVersion() + ".");
83 String jvmmempc = null;
84 String jvmmemmax = null;
85 boolean debug = false;
87 boolean quiet = false;
88 boolean headless = false;
89 boolean assumeheadless = false;
92 boolean stdout = false;
93 // must set --debug before --launcher...
94 boolean launcherstop = false;
95 boolean launcherprint = false;
96 boolean launcherwait = false;
97 ArrayList<String> arguments = new ArrayList<>();
98 String previousArg = null;
100 for (String arg : args)
102 if (arg.equals("--debug"))
107 for (String arg : args)
109 if (arg.equals("--quiet"))
113 else if (arg.equals("--gui"))
117 else if (arg.equals("--help"))
121 else if (arg.equals("--version"))
128 for (String a : assumeHeadlessArgs)
130 if (arg.equals("--" + a) || arg.startsWith("--" + a + "="))
132 assumeheadless = true;
137 if (arg.equals("--output=-")
138 || (arg.equals("-") && "--output".equals(previousArg)))
145 if (arg.equals("--launcherprint"))
147 launcherprint = true;
149 else if (arg.equals("--launcherstop"))
153 else if (arg.equals("--launcherwait"))
158 // this ends the launcher immediately
159 if (arg.equals("--launchernowait"))
165 // Don't add the --launcher... args to Jalview launch
166 if (arg.startsWith("--launcher"))
170 // jvmmempc and jvmmemmax args used to set memory and are not passed on to
172 final String jvmmempcArg = Arg.JVMMEMPC.getName();
173 final String jvmmemmaxArg = Arg.JVMMEMMAX.getName();
174 if (arg.startsWith("-" + jvmmempcArg + "="))
176 jvmmempc = arg.substring(jvmmempcArg.length() + 2);
178 else if (arg.startsWith("-" + jvmmemmaxArg + "="))
180 jvmmemmax = arg.substring(jvmmemmaxArg.length() + 2);
182 // --doubledash versions
183 else if (arg.startsWith("--" + jvmmempcArg + "="))
185 jvmmempc = arg.substring(jvmmempcArg.length() + 3);
187 else if (arg.startsWith("--" + jvmmemmaxArg + "="))
189 jvmmemmax = arg.substring(jvmmemmaxArg.length() + 3);
199 // --help takes precedence over --gui
204 // --gui takes precedence over --headless
209 // --output arguments assume headless mode
210 headless = assumeheadless;
213 final String appName = ChannelProperties.getProperty("app_name");
215 // if we're using jalview.bin.Launcher we always assume a console is in use
216 final String javaBin = LaunchUtils.findJavaBin(true);
218 List<String> command = new ArrayList<>();
219 command.add(javaBin);
221 String memSetting = null;
223 for (String jvmArg : ManagementFactory.getRuntimeMXBean()
224 .getInputArguments())
229 command.add(ManagementFactory.getRuntimeMXBean().getClassPath());
231 // use saved preferences if no cmdline args
232 boolean useCustomisedSettings = LaunchUtils
233 .getBooleanUserPreference(MemorySetting.CUSTOMISED_SETTINGS);
234 if (useCustomisedSettings)
236 if (jvmmempc == null)
238 jvmmempc = LaunchUtils
239 .getUserPreference(MemorySetting.MEMORY_JVMMEMPC);
241 if (jvmmemmax == null)
243 jvmmemmax = LaunchUtils
244 .getUserPreference(MemorySetting.MEMORY_JVMMEMMAX);
248 // add these settings if not already specified
249 boolean memSet = false;
250 boolean dockIcon = false;
251 boolean dockName = false;
252 boolean headlessProp = false;
253 boolean macosHeadlessProp = false;
254 for (int i = 0; i < command.size(); i++)
256 String arg = command.get(i);
257 if (arg.startsWith("-Xmx"))
259 // only use -Xmx if jvmmemmax and jvmmempc have not been set
260 if (jvmmempc == null && jvmmemmax == null)
266 else if (arg.startsWith("-Xdock:icon"))
270 else if (arg.startsWith("-Xdock:name"))
274 else if (arg.startsWith("-D" + headlessProperty + "="))
278 else if (arg.startsWith("-D" + macosHeadlessProperty + "="))
280 macosHeadlessProp = true;
286 long maxMemLong = MemorySetting.getMemorySetting(jvmmemmax, jvmmempc);
290 memSetting = "-Xmx" + Long.toString(maxMemLong);
292 command.add(memSetting);
296 if (LaunchUtils.isMac)
300 String dockIconPath = System.getProperty("getdownappdir", ".")
301 + File.separator + "resource/jalview_logo.png";
302 command.add("-Xdock:icon=" + dockIconPath);
306 // -Xdock:name=... doesn't actually work :(
307 // Leaving it in in case it gets fixed
308 command.add("-Xdock:name=" + appName);
309 // This also does not work for the dock
310 command.add("-Dcom.apple.mrj.application.apple.menu.about.name="
314 if (headless && !headlessProp)
316 System.setProperty(headlessProperty, "true");
317 /* not setting this in java invocation of running jalview due to problem with Jmol */
320 command.add("-D" + headlessProperty + "=true");
323 if (headless && LaunchUtils.isMac && !macosHeadlessProp)
325 System.setProperty(macosHeadlessProperty, "true");
326 command.add("-D" + macosHeadlessProperty + "=true");
329 String scalePropertyArg = HiDPISetting.getScalePropertyArg();
330 if (scalePropertyArg != null)
332 syserr(debug, quiet, "Running " + startClass + " with scale setting "
334 command.add(scalePropertyArg);
337 command.add(startClass);
338 command.addAll(arguments);
340 final ProcessBuilder builder = new ProcessBuilder(command);
342 if ((Boolean.parseBoolean(System.getProperty("launcherprint", "false"))
346 "LAUNCHER COMMAND: " + String.join(" ", builder.command()));
349 "Running " + startClass + " with "
350 + (memSetting == null ? "no memory setting"
351 : ("memory setting " + memSetting)));
353 if (Boolean.parseBoolean(System.getProperty("launcherstop", "false"))
354 || (debug && launcherstop))
357 "System property 'launcherstop' is set and not 'false'. Exiting.");
363 Process process = builder.start();
364 if (wait || launcherwait)
366 syserr(debug, quiet, "Launching application process");
373 "Wait time for application process is " + waitInt + "ms");
374 process.waitFor(waitInt, TimeUnit.MILLISECONDS);
376 syserr(debug, quiet, "Launcher process ending");
377 } catch (IOException e)
379 if (e.getMessage().toLowerCase(Locale.ROOT).contains("memory"))
382 .errPrintln("Caught a memory exception: " + e.getMessage());
383 // Probably the "Cannot allocate memory" error, try without the memory
385 ArrayList<String> commandNoMem = new ArrayList<>();
386 for (int i = 0; i < command.size(); i++)
388 if (!command.get(i).startsWith("-Xmx"))
390 commandNoMem.add(command.get(i));
393 final ProcessBuilder builderNoMem = new ProcessBuilder(
395 jalview.bin.Console.errPrintln("Command without memory setting: "
396 + String.join(" ", builderNoMem.command()));
399 builderNoMem.inheritIO();
400 Process processNoMem = builderNoMem.start();
401 processNoMem.waitFor();
402 } catch (Exception ex)
404 ex.printStackTrace();
411 } catch (Exception e)
417 private static void syserr(boolean debug, boolean quiet, String message)
421 jalview.bin.Console.errPrintln("LAUNCHERDEBUG - " + message);