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 // not setting this yet due to problem with Jmol
54 private final static String headlessProperty = "java.awt.headless";
56 // used for headless macOS
57 private final static String macosHeadlessProperty = "apple.awt.UIElement";
59 // arguments that assume headless mode
60 private final static String[] assumeHeadlessArgs = { "headless", "output",
61 "image", "structureimage" };
64 * main method for jalview.bin.Launcher. This restarts the same JRE's JVM with
65 * the same arguments but with memory adjusted based on extracted -jvmmempc
66 * and -jvmmemmax application arguments. If on a Mac then extra dock:icon and
67 * dock:name arguments are also set.
71 public static void main(String[] args)
73 if (!LaunchUtils.checkJavaVersion())
76 .errPrintln("WARNING - The Java version being used (Java "
77 + LaunchUtils.getJavaVersion()
78 + ") may lead to problems. This installation of Jalview should be used with Java "
79 + LaunchUtils.getJavaCompileVersion() + ".");
82 String jvmmempc = null;
83 String jvmmemmax = null;
84 boolean debug = false;
86 boolean quiet = false;
87 boolean headless = false;
88 boolean assumeheadless = false;
91 boolean stdout = false;
92 // must set --debug before --launcher...
93 boolean launcherstop = false;
94 boolean launcherprint = false;
95 boolean launcherwait = false;
96 ArrayList<String> arguments = new ArrayList<>();
97 String previousArg = null;
99 for (String arg : args)
101 if (arg.equals("--debug"))
106 for (String arg : args)
108 if (arg.equals("--quiet"))
112 else if (arg.equals("--gui"))
116 else if (arg.equals("--help"))
120 else if (arg.equals("--version"))
127 for (String a : assumeHeadlessArgs)
129 if (arg.equals("--" + a) || arg.startsWith("--" + a + "="))
131 assumeheadless = true;
136 if (arg.equals("--output=-")
137 || (arg.equals("-") && "--output".equals(previousArg)))
144 if (arg.equals("--launcherprint"))
146 launcherprint = true;
148 else if (arg.equals("--launcherstop"))
152 else if (arg.equals("--launcherwait"))
157 // this ends the launcher immediately
158 if (arg.equals("--launchernowait"))
164 // Don't add the --launcher... args to Jalview launch
165 if (arg.startsWith("--launcher"))
169 // jvmmempc and jvmmemmax args used to set memory and are not passed on to
172 "-" + MemorySetting.MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "="))
174 jvmmempc = arg.substring(
175 MemorySetting.MAX_HEAPSIZE_PERCENT_PROPERTY_NAME.length()
178 else if (arg.startsWith(
179 "-" + MemorySetting.MAX_HEAPSIZE_PROPERTY_NAME + "="))
181 jvmmemmax = arg.substring(
182 MemorySetting.MAX_HEAPSIZE_PROPERTY_NAME.length() + 2);
184 // --doubledash versions
185 else if (arg.startsWith("--"
186 + MemorySetting.MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "="))
188 jvmmempc = arg.substring(
189 MemorySetting.MAX_HEAPSIZE_PERCENT_PROPERTY_NAME.length()
192 else if (arg.startsWith(
193 "--" + MemorySetting.MAX_HEAPSIZE_PROPERTY_NAME + "="))
195 jvmmemmax = arg.substring(
196 MemorySetting.MAX_HEAPSIZE_PROPERTY_NAME.length() + 3);
206 // --help takes precedence over --gui
211 // --gui takes precedence over --headless
216 // --output arguments assume headless mode
217 headless = assumeheadless;
220 final String appName = ChannelProperties.getProperty("app_name");
222 // if we're using jalview.bin.Launcher we always assume a console is in use
223 final String javaBin = LaunchUtils.findJavaBin(true);
225 List<String> command = new ArrayList<>();
226 command.add(javaBin);
228 String memSetting = null;
230 for (String jvmArg : ManagementFactory.getRuntimeMXBean()
231 .getInputArguments())
236 command.add(ManagementFactory.getRuntimeMXBean().getClassPath());
238 // use saved preferences if no cmdline args
239 boolean useCustomisedSettings = LaunchUtils
240 .getBooleanUserPreference(MemorySetting.CUSTOMISED_SETTINGS);
241 if (useCustomisedSettings)
243 if (jvmmempc == null)
245 jvmmempc = LaunchUtils
246 .getUserPreference(MemorySetting.MEMORY_JVMMEMPC);
248 if (jvmmemmax == null)
250 jvmmemmax = LaunchUtils
251 .getUserPreference(MemorySetting.MEMORY_JVMMEMMAX);
255 // add these settings if not already specified
256 boolean memSet = false;
257 boolean dockIcon = false;
258 boolean dockName = false;
259 boolean headlessProp = false;
260 boolean macosHeadlessProp = false;
261 for (int i = 0; i < command.size(); i++)
263 String arg = command.get(i);
264 if (arg.startsWith("-Xmx"))
266 // only use -Xmx if jvmmemmax and jvmmempc have not been set
267 if (jvmmempc == null && jvmmemmax == null)
273 else if (arg.startsWith("-Xdock:icon"))
277 else if (arg.startsWith("-Xdock:name"))
281 else if (arg.startsWith("-D" + headlessProperty + "="))
285 else if (arg.startsWith("-D" + macosHeadlessProperty + "="))
287 macosHeadlessProp = true;
293 long maxMemLong = MemorySetting.getMemorySetting(jvmmemmax, jvmmempc);
297 memSetting = "-Xmx" + Long.toString(maxMemLong);
299 command.add(memSetting);
303 if (LaunchUtils.isMac)
307 String dockIconPath = System.getProperty("getdownappdir", ".")
308 + File.separator + "resource/jalview_logo.png";
309 command.add("-Xdock:icon=" + dockIconPath);
313 // -Xdock:name=... doesn't actually work :(
314 // Leaving it in in case it gets fixed
315 command.add("-Xdock:name=" + appName);
316 // This also does not work for the dock
317 command.add("-Dcom.apple.mrj.application.apple.menu.about.name="
321 if (headless && !headlessProp)
323 System.setProperty(headlessProperty, "true");
324 /* not setting this in java invocation of running jalview due to problem with Jmol */
327 command.add("-D" + headlessProperty + "=true");
330 if (headless && LaunchUtils.isMac && !macosHeadlessProp)
332 System.setProperty(macosHeadlessProperty, "true");
333 command.add("-D" + macosHeadlessProperty + "=true");
336 String scalePropertyArg = HiDPISetting.getScalePropertyArg();
337 if (scalePropertyArg != null)
339 syserr(debug, quiet, "Running " + startClass + " with scale setting "
341 command.add(scalePropertyArg);
344 command.add(startClass);
345 command.addAll(arguments);
347 final ProcessBuilder builder = new ProcessBuilder(command);
349 if ((Boolean.parseBoolean(System.getProperty("launcherprint", "false"))
353 "LAUNCHER COMMAND: " + String.join(" ", builder.command()));
356 "Running " + startClass + " with "
357 + (memSetting == null ? "no memory setting"
358 : ("memory setting " + memSetting)));
360 if (Boolean.parseBoolean(System.getProperty("launcherstop", "false"))
361 || (debug && launcherstop))
364 "System property 'launcherstop' is set and not 'false'. Exiting.");
370 Process process = builder.start();
371 if (wait || launcherwait)
373 syserr(debug, quiet, "Launching application process");
380 "Wait time for application process is " + waitInt + "ms");
381 process.waitFor(waitInt, TimeUnit.MILLISECONDS);
383 syserr(debug, quiet, "Launcher process ending");
384 } catch (IOException e)
386 if (e.getMessage().toLowerCase(Locale.ROOT).contains("memory"))
389 .errPrintln("Caught a memory exception: " + e.getMessage());
390 // Probably the "Cannot allocate memory" error, try without the memory
392 ArrayList<String> commandNoMem = new ArrayList<>();
393 for (int i = 0; i < command.size(); i++)
395 if (!command.get(i).startsWith("-Xmx"))
397 commandNoMem.add(command.get(i));
400 final ProcessBuilder builderNoMem = new ProcessBuilder(
402 jalview.bin.Console.errPrintln("Command without memory setting: "
403 + String.join(" ", builderNoMem.command()));
406 builderNoMem.inheritIO();
407 Process processNoMem = builderNoMem.start();
408 processNoMem.waitFor();
409 } catch (Exception ex)
411 ex.printStackTrace();
418 } catch (Exception e)
424 private static void syserr(boolean debug, boolean quiet, String message)
428 jalview.bin.Console.errPrintln("LAUNCHERDEBUG - " + message);