4 import java.io.IOException;
5 import java.lang.management.ManagementFactory;
6 import java.util.ArrayList;
10 * A Launcher class for Jalview. This class is used to launch Jalview from the
11 * shadowJar when Getdown is not used or available. It attempts to take all the
12 * command line arguments to pass on to the jalview.bin.Jalview class, but to
13 * insert a -Xmx memory setting to a sensible default, using the -jvmmempc and
14 * -jvmmemmax application arguments if specified. If not specified then system
15 * properties will be looked for by jalview.bin.MemorySetting. If the user has
16 * provided the JVM with a -Xmx setting directly and not set -jvmmempc or
17 * -jvmmemmax then this setting will be used and system properties ignored. If
18 * -Xmx is set as well as -jvmmempc or -jvmmemmax as argument(s) then the -Xmx
19 * argument will NOT be passed on to the main application launch.
26 private final static String startClass = "jalview.bin.Jalview";
28 private final static String dockIconPath = "JalviewLogo_Huge.png";
31 * main method for jalview.bin.Launcher. This restarts the same JRE's JVM with
32 * the same arguments but with memory adjusted based on extracted -jvmmempc and
33 * -jvmmemmax application arguments. If on a Mac then extra dock:icon and
34 * dock:name arguments are also set.
38 public static void main(String[] args)
40 final String javaBin = System.getProperty("java.home") + File.separator
41 + "bin" + File.separator + "java";
43 List<String> command = new ArrayList<>();
46 String memSetting = null;
48 boolean isAMac = System.getProperty("os.name").indexOf("Mac") > -1;
50 for (String jvmArg : ManagementFactory.getRuntimeMXBean()
56 command.add(ManagementFactory.getRuntimeMXBean().getClassPath());
58 String jvmmempc = null;
59 String jvmmemmax = null;
60 ArrayList<String> arguments = new ArrayList<>();
61 for (String arg : args)
63 // jvmmempc and jvmmemmax args used to set memory and are not passed on to
66 "-" + MemorySetting.MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "="))
68 jvmmempc = arg.substring(
69 MemorySetting.MAX_HEAPSIZE_PERCENT_PROPERTY_NAME.length()
72 else if (arg.startsWith(
73 "-" + MemorySetting.MAX_HEAPSIZE_PROPERTY_NAME + "="))
75 jvmmemmax = arg.substring(
76 MemorySetting.MAX_HEAPSIZE_PROPERTY_NAME.length() + 2);
84 // add memory setting if not specified
85 boolean memSet = false;
86 boolean dockIcon = false;
87 boolean dockName = false;
88 for (int i = 0; i < command.size(); i++)
90 String arg = command.get(i);
91 if (arg.startsWith("-Xmx"))
93 // only use -Xmx if jvmmemmax and jvmmempc have not been set
94 if (jvmmempc == null && jvmmemmax == null)
100 else if (arg.startsWith("-Xdock:icon"))
104 else if (arg.startsWith("-Xdock:name"))
112 long maxMemLong = MemorySetting.getMemorySetting(jvmmemmax, jvmmempc);
116 memSetting = "-Xmx" + Long.toString(maxMemLong);
118 command.add(memSetting);
126 command.add("-Xdock:icon=" + dockIconPath);
130 // -Xdock:name=... doesn't actually work :(
131 // Leaving it in in case it gets fixed
132 command.add("-Xdock:name=" + "Jalview");
136 command.add(startClass);
137 command.addAll(arguments);
139 final ProcessBuilder builder = new ProcessBuilder(command);
141 // System.out.println("COMMAND: " + String.join(" ", builder.command()));
142 System.out.println("Running " + startClass + " with "
143 + (memSetting == null ? "no memory setting" : memSetting));
145 if (Boolean.parseBoolean(System.getProperty("launcherstop")))
152 Process process = builder.start();
154 } catch (IOException e)
156 if (e.getMessage().toLowerCase().contains("memory"))
158 System.out.println("Caught a memory exception: " + e.getMessage());
159 // Probably the "Cannot allocate memory" error, try without the memory setting
160 ArrayList<String> commandNoMem = new ArrayList<>();
161 for (int i = 0; i < command.size(); i++)
163 if (!command.get(i).startsWith("-Xmx"))
165 commandNoMem.add(command.get(i));
168 final ProcessBuilder builderNoMem = new ProcessBuilder(
170 System.out.println("Command without memory setting: "
171 + String.join(" ", builderNoMem.command()));
174 builderNoMem.inheritIO();
175 Process processNoMem = builderNoMem.start();
176 processNoMem.waitFor();
177 } catch (Exception ex)
179 ex.printStackTrace();
186 } catch (Exception e)