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 static boolean checkJVMSymlink(String testBin)
55 File testBinFile = new File(testBin);
56 if (!testBinFile.exists())
60 File targetFile = null;
63 targetFile = testBinFile.getCanonicalFile();
64 } catch (IOException e)
68 if (targetFile != null && ("java".equals(targetFile.getName())
69 || "java.exe".equals(targetFile.getName())))
77 * main method for jalview.bin.Launcher. This restarts the same JRE's JVM with
78 * the same arguments but with memory adjusted based on extracted -jvmmempc
79 * and -jvmmemmax application arguments. If on a Mac then extra dock:icon and
80 * dock:name arguments are also set.
84 public static void main(String[] args)
86 if (!LaunchUtils.checkJavaVersion())
88 System.err.println("WARNING - The Java version being used (Java "
89 + LaunchUtils.getJavaVersion()
90 + ") may lead to problems. This installation of Jalview should be used with Java "
91 + LaunchUtils.getJavaCompileVersion() + ".");
93 final String appName = ChannelProperties.getProperty("app_name");
94 final String javaBinDir = System.getProperty("java.home")
95 + File.separator + "bin" + File.separator;
96 String javaBin = null;
97 if (javaBin == null && checkJVMSymlink(javaBinDir + appName))
99 javaBin = javaBinDir + appName;
101 if (javaBin == null && checkJVMSymlink(javaBinDir + "Jalview"))
103 javaBin = javaBinDir + "Jalview";
110 List<String> command = new ArrayList<>();
111 command.add(javaBin);
113 String memSetting = null;
115 boolean isAMac = System.getProperty("os.name").indexOf("Mac") > -1;
117 for (String jvmArg : ManagementFactory.getRuntimeMXBean()
118 .getInputArguments())
123 command.add(ManagementFactory.getRuntimeMXBean().getClassPath());
125 String jvmmempc = null;
126 String jvmmemmax = null;
127 boolean debug = false;
129 boolean quiet = false;
130 // must set --debug before --launcher...
131 boolean launcherstop = false;
132 boolean launcherprint = false;
133 boolean launcherwait = false;
134 ArrayList<String> arguments = new ArrayList<>();
135 for (String arg : args)
137 if (arg.equals("--debug"))
141 if (arg.equals("--quiet"))
145 if (debug && arg.equals("--launcherprint"))
147 launcherprint = true;
149 if (debug && arg.equals("--launcherstop"))
153 if (debug && arg.equals("--launcherwait"))
157 // this ends the launcher immediately
158 if (debug && arg.equals("--launchernowait"))
162 // Don't add the --launcher... args to Jalview launch
163 if (arg.startsWith("--launcher"))
167 // jvmmempc and jvmmemmax args used to set memory and are not passed on to
170 "-" + MemorySetting.MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "="))
172 jvmmempc = arg.substring(
173 MemorySetting.MAX_HEAPSIZE_PERCENT_PROPERTY_NAME.length()
176 else if (arg.startsWith(
177 "-" + MemorySetting.MAX_HEAPSIZE_PROPERTY_NAME + "="))
179 jvmmemmax = arg.substring(
180 MemorySetting.MAX_HEAPSIZE_PROPERTY_NAME.length() + 2);
182 // --doubledash versions
183 else if (arg.startsWith("--"
184 + MemorySetting.MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "="))
186 jvmmempc = arg.substring(
187 MemorySetting.MAX_HEAPSIZE_PERCENT_PROPERTY_NAME.length()
190 else if (arg.startsWith(
191 "--" + MemorySetting.MAX_HEAPSIZE_PROPERTY_NAME + "="))
193 jvmmemmax = arg.substring(
194 MemorySetting.MAX_HEAPSIZE_PROPERTY_NAME.length() + 3);
203 // use saved preferences if no cmdline args
204 boolean useCustomisedSettings = LaunchUtils
205 .getBooleanUserPreference(MemorySetting.CUSTOMISED_SETTINGS);
206 if (useCustomisedSettings)
208 if (jvmmempc == null)
210 jvmmempc = LaunchUtils
211 .getUserPreference(MemorySetting.MEMORY_JVMMEMPC);
213 if (jvmmemmax == null)
215 jvmmemmax = LaunchUtils
216 .getUserPreference(MemorySetting.MEMORY_JVMMEMMAX);
220 // add these settings if not already specified
221 boolean memSet = false;
222 boolean dockIcon = false;
223 boolean dockName = false;
224 for (int i = 0; i < command.size(); i++)
226 String arg = command.get(i);
227 if (arg.startsWith("-Xmx"))
229 // only use -Xmx if jvmmemmax and jvmmempc have not been set
230 if (jvmmempc == null && jvmmemmax == null)
236 else if (arg.startsWith("-Xdock:icon"))
240 else if (arg.startsWith("-Xdock:name"))
248 long maxMemLong = MemorySetting.getMemorySetting(jvmmemmax, jvmmempc);
252 memSetting = "-Xmx" + Long.toString(maxMemLong);
254 command.add(memSetting);
262 String dockIconPath = System.getProperty("getdownappdir", ".")
263 + File.separator + "resource/jalview_logo.png";
264 command.add("-Xdock:icon=" + dockIconPath);
268 // -Xdock:name=... doesn't actually work :(
269 // Leaving it in in case it gets fixed
270 command.add("-Xdock:name=" + appName);
271 // this launches WITHOUT an icon in the macOS dock. Could be useful for
273 // command.add("-Dapple.awt.UIElement=false");
274 // This also does not work for the dock
275 command.add("-Dcom.apple.mrj.application.apple.menu.about.name="
280 String scalePropertyArg = HiDPISetting.getScalePropertyArg();
281 if (scalePropertyArg != null)
283 sysout(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 sysout(debug, quiet, "Launching application process");
324 "Wait time for application process is " + waitInt + "ms");
325 process.waitFor(waitInt, TimeUnit.MILLISECONDS);
327 sysout(debug, quiet, "Launcher process ending");
328 } catch (IOException e)
330 if (e.getMessage().toLowerCase(Locale.ROOT).contains("memory"))
332 System.err.println("Caught a memory exception: " + e.getMessage());
333 // Probably the "Cannot allocate memory" error, try without the memory
335 ArrayList<String> commandNoMem = new ArrayList<>();
336 for (int i = 0; i < command.size(); i++)
338 if (!command.get(i).startsWith("-Xmx"))
340 commandNoMem.add(command.get(i));
343 final ProcessBuilder builderNoMem = new ProcessBuilder(
345 System.err.println("Command without memory setting: "
346 + String.join(" ", builderNoMem.command()));
349 builderNoMem.inheritIO();
350 Process processNoMem = builderNoMem.start();
351 processNoMem.waitFor();
352 } catch (Exception ex)
354 ex.printStackTrace();
361 } catch (Exception e)
367 private static void sysout(boolean debug, boolean quiet, String message)
371 System.out.println("LAUNCHERDEBUG - " + message);