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;
30 import jalview.util.ChannelProperties;
31 import jalview.util.LaunchUtils;
34 * A Launcher class for Jalview. This class is used to launch Jalview from the
35 * shadowJar when Getdown is not used or available. It attempts to take all the
36 * command line arguments to pass on to the jalview.bin.Jalview class, but to
37 * insert a -Xmx memory setting to a sensible default, using the -jvmmempc and
38 * -jvmmemmax application arguments if specified. If not specified then system
39 * properties will be looked for by jalview.bin.MemorySetting. If the user has
40 * provided the JVM with a -Xmx setting directly and not set -jvmmempc or
41 * -jvmmemmax then this setting will be used and system properties ignored. If
42 * -Xmx is set as well as -jvmmempc or -jvmmemmax as argument(s) then the -Xmx
43 * argument will NOT be passed on to the main application launch.
50 private final static String startClass = "jalview.bin.Jalview";
52 private final static String dockIconPath = ChannelProperties
53 .getProperty("logo.512");
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 final String javaBin = System.getProperty("java.home") + File.separator
74 + "bin" + File.separator + "java";
76 List<String> command = new ArrayList<>();
79 String memSetting = null;
81 boolean isAMac = System.getProperty("os.name").indexOf("Mac") > -1;
83 for (String jvmArg : ManagementFactory.getRuntimeMXBean()
89 command.add(ManagementFactory.getRuntimeMXBean().getClassPath());
91 String jvmmempc = null;
92 String jvmmemmax = null;
93 ArrayList<String> arguments = new ArrayList<>();
94 for (String arg : args)
96 // jvmmempc and jvmmemmax args used to set memory and are not passed on to
99 "-" + MemorySetting.MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "="))
101 jvmmempc = arg.substring(
102 MemorySetting.MAX_HEAPSIZE_PERCENT_PROPERTY_NAME.length()
105 else if (arg.startsWith(
106 "-" + MemorySetting.MAX_HEAPSIZE_PROPERTY_NAME + "="))
108 jvmmemmax = arg.substring(
109 MemorySetting.MAX_HEAPSIZE_PROPERTY_NAME.length() + 2);
117 // use saved preferences if no cmdline args
118 boolean useCustomisedSettings = LaunchUtils
119 .getBooleanUserPreference(MemorySetting.CUSTOMISED_SETTINGS);
120 if (useCustomisedSettings)
122 if (jvmmempc == null)
124 jvmmempc = LaunchUtils
125 .getUserPreference(MemorySetting.MEMORY_JVMMEMPC);
127 if (jvmmemmax == null)
129 jvmmemmax = LaunchUtils
130 .getUserPreference(MemorySetting.MEMORY_JVMMEMMAX);
134 // add memory setting if not specified
135 boolean memSet = false;
136 boolean dockIcon = false;
137 boolean dockName = false;
138 for (int i = 0; i < command.size(); i++)
140 String arg = command.get(i);
141 if (arg.startsWith("-Xmx"))
143 // only use -Xmx if jvmmemmax and jvmmempc have not been set
144 if (jvmmempc == null && jvmmemmax == null)
150 else if (arg.startsWith("-Xdock:icon"))
154 else if (arg.startsWith("-Xdock:name"))
162 long maxMemLong = MemorySetting.getMemorySetting(jvmmemmax, jvmmempc);
166 memSetting = "-Xmx" + Long.toString(maxMemLong);
168 command.add(memSetting);
176 command.add("-Xdock:icon=" + dockIconPath);
180 // -Xdock:name=... doesn't actually work :(
181 // Leaving it in in case it gets fixed
183 "-Xdock:name=" + ChannelProperties.getProperty("app_name"));
187 String scalePropertyArg = HiDPISetting.getScalePropertyArg();
188 if (scalePropertyArg != null)
190 System.out.println("Running " + startClass + " with scale setting "
192 command.add(scalePropertyArg);
195 command.add(startClass);
196 command.addAll(arguments);
198 final ProcessBuilder builder = new ProcessBuilder(command);
200 if (Boolean.parseBoolean(System.getProperty("launcherprint", "false")))
203 "LAUNCHER COMMAND: " + String.join(" ", builder.command()));
205 System.out.println("Running " + startClass + " with "
206 + (memSetting == null ? "no memory setting"
207 : ("memory setting " + memSetting)));
209 if (Boolean.parseBoolean(System.getProperty("launcherstop", "false")))
216 Process process = builder.start();
218 } catch (IOException e)
220 if (e.getMessage().toLowerCase(Locale.ROOT).contains("memory"))
222 System.out.println("Caught a memory exception: " + e.getMessage());
223 // Probably the "Cannot allocate memory" error, try without the memory
225 ArrayList<String> commandNoMem = new ArrayList<>();
226 for (int i = 0; i < command.size(); i++)
228 if (!command.get(i).startsWith("-Xmx"))
230 commandNoMem.add(command.get(i));
233 final ProcessBuilder builderNoMem = new ProcessBuilder(
235 System.out.println("Command without memory setting: "
236 + String.join(" ", builderNoMem.command()));
239 builderNoMem.inheritIO();
240 Process processNoMem = builderNoMem.start();
241 processNoMem.waitFor();
242 } catch (Exception ex)
244 ex.printStackTrace();
251 } catch (Exception e)