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())
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() + ".");
73 String jvmmempc = null;
74 String jvmmemmax = null;
75 boolean debug = false;
77 boolean quiet = false;
78 boolean headless = false;
80 // must set --debug before --launcher...
81 boolean launcherstop = false;
82 boolean launcherprint = false;
83 boolean launcherwait = false;
84 ArrayList<String> arguments = new ArrayList<>();
85 for (String arg : args)
87 if (arg.equals("--debug"))
91 if (arg.equals("--quiet"))
95 if (arg.equals("--headless"))
99 if (arg.equals("--gui"))
103 if (debug && arg.equals("--launcherprint"))
105 launcherprint = true;
107 if (debug && arg.equals("--launcherstop"))
111 if (debug && arg.equals("--launcherwait"))
115 // this ends the launcher immediately
116 if (debug && arg.equals("--launchernowait"))
120 // Don't add the --launcher... args to Jalview launch
121 if (arg.startsWith("--launcher"))
125 // jvmmempc and jvmmemmax args used to set memory and are not passed on to
128 "-" + MemorySetting.MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "="))
130 jvmmempc = arg.substring(
131 MemorySetting.MAX_HEAPSIZE_PERCENT_PROPERTY_NAME.length()
134 else if (arg.startsWith(
135 "-" + MemorySetting.MAX_HEAPSIZE_PROPERTY_NAME + "="))
137 jvmmemmax = arg.substring(
138 MemorySetting.MAX_HEAPSIZE_PROPERTY_NAME.length() + 2);
140 // --doubledash versions
141 else if (arg.startsWith("--"
142 + MemorySetting.MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "="))
144 jvmmempc = arg.substring(
145 MemorySetting.MAX_HEAPSIZE_PERCENT_PROPERTY_NAME.length()
148 else if (arg.startsWith(
149 "--" + MemorySetting.MAX_HEAPSIZE_PROPERTY_NAME + "="))
151 jvmmemmax = arg.substring(
152 MemorySetting.MAX_HEAPSIZE_PROPERTY_NAME.length() + 3);
162 // --gui takes precedence over --headless
166 final String appName = ChannelProperties.getProperty("app_name");
168 // if we're using jalview.bin.Launcher we always assume a console is in use
169 final String javaBin = LaunchUtils.findJavaBin(true);
171 List<String> command = new ArrayList<>();
172 command.add(javaBin);
174 String memSetting = null;
176 for (String jvmArg : ManagementFactory.getRuntimeMXBean()
177 .getInputArguments())
182 command.add(ManagementFactory.getRuntimeMXBean().getClassPath());
184 // use saved preferences if no cmdline args
185 boolean useCustomisedSettings = LaunchUtils
186 .getBooleanUserPreference(MemorySetting.CUSTOMISED_SETTINGS);
187 if (useCustomisedSettings)
189 if (jvmmempc == null)
191 jvmmempc = LaunchUtils
192 .getUserPreference(MemorySetting.MEMORY_JVMMEMPC);
194 if (jvmmemmax == null)
196 jvmmemmax = LaunchUtils
197 .getUserPreference(MemorySetting.MEMORY_JVMMEMMAX);
201 // add these settings if not already specified
202 boolean memSet = false;
203 boolean dockIcon = false;
204 boolean dockName = false;
205 boolean headlessProp = false;
206 for (int i = 0; i < command.size(); i++)
208 String arg = command.get(i);
209 if (arg.startsWith("-Xmx"))
211 // only use -Xmx if jvmmemmax and jvmmempc have not been set
212 if (jvmmempc == null && jvmmemmax == null)
218 else if (arg.startsWith("-Xdock:icon"))
222 else if (arg.startsWith("-Xdock:name"))
226 else if (arg.startsWith("-D" + headlessProperty + "="))
234 long maxMemLong = MemorySetting.getMemorySetting(jvmmemmax, jvmmempc);
238 memSetting = "-Xmx" + Long.toString(maxMemLong);
240 command.add(memSetting);
244 if (LaunchUtils.isMac)
248 String dockIconPath = System.getProperty("getdownappdir", ".")
249 + File.separator + "resource/jalview_logo.png";
250 command.add("-Xdock:icon=" + dockIconPath);
254 // -Xdock:name=... doesn't actually work :(
255 // Leaving it in in case it gets fixed
256 command.add("-Xdock:name=" + appName);
257 // this launches WITHOUT an icon in the macOS dock. Could be useful for
259 // command.add("-Dapple.awt.UIElement=false");
260 // This also does not work for the dock
261 command.add("-Dcom.apple.mrj.application.apple.menu.about.name="
265 if (headless && !headlessProp)
267 System.setProperty(headlessProperty, "true");
268 command.add("-D" + headlessProperty + "=true");
271 String scalePropertyArg = HiDPISetting.getScalePropertyArg();
272 if (scalePropertyArg != null)
274 sysout(debug, quiet, "Running " + startClass + " with scale setting "
276 command.add(scalePropertyArg);
279 command.add(startClass);
280 command.addAll(arguments);
282 final ProcessBuilder builder = new ProcessBuilder(command);
284 if ((Boolean.parseBoolean(System.getProperty("launcherprint", "false"))
288 "LAUNCHER COMMAND: " + String.join(" ", builder.command()));
291 "Running " + startClass + " with "
292 + (memSetting == null ? "no memory setting"
293 : ("memory setting " + memSetting)));
295 if (Boolean.parseBoolean(System.getProperty("launcherstop", "false"))
296 || (debug && launcherstop))
299 "System property 'launcherstop' is set and not 'false'. Exiting.");
305 Process process = builder.start();
306 if (wait || launcherwait)
308 sysout(debug, quiet, "Launching application process");
315 "Wait time for application process is " + waitInt + "ms");
316 process.waitFor(waitInt, TimeUnit.MILLISECONDS);
318 sysout(debug, quiet, "Launcher process ending");
319 } catch (IOException e)
321 if (e.getMessage().toLowerCase(Locale.ROOT).contains("memory"))
323 System.err.println("Caught a memory exception: " + e.getMessage());
324 // Probably the "Cannot allocate memory" error, try without the memory
326 ArrayList<String> commandNoMem = new ArrayList<>();
327 for (int i = 0; i < command.size(); i++)
329 if (!command.get(i).startsWith("-Xmx"))
331 commandNoMem.add(command.get(i));
334 final ProcessBuilder builderNoMem = new ProcessBuilder(
336 System.err.println("Command without memory setting: "
337 + String.join(" ", builderNoMem.command()));
340 builderNoMem.inheritIO();
341 Process processNoMem = builderNoMem.start();
342 processNoMem.waitFor();
343 } catch (Exception ex)
345 ex.printStackTrace();
352 } catch (Exception e)
358 private static void sysout(boolean debug, boolean quiet, String message)
362 System.out.println("LAUNCHERDEBUG - " + message);