4 import java.io.IOException;
5 import java.lang.management.ManagementFactory;
6 import java.util.ArrayList;
11 private final static String startClass = "jalview.bin.Jalview";
13 private final static int maxHeapSizePerCent = 90;
15 private final static String maxHeapSizePerCentProperty = "jvmmempc";
17 private final static String dockIconPath = "JalviewLogo_Huge.png";
19 public static void main(String[] args)
21 final String javaBin = System.getProperty("java.home") + File.separator
22 + "bin" + File.separator + "java";
24 ArrayList<String> command = new ArrayList<>();
27 String memSetting = null;
29 boolean isAMac = System.getProperty("os.name").indexOf("Mac") > -1;
31 for (String jvmArg : ManagementFactory.getRuntimeMXBean()
37 command.add(ManagementFactory.getRuntimeMXBean().getClassPath());
38 ArrayList<String> arguments = new ArrayList<>();
39 for (String arg : args)
44 // add memory setting if not specified
45 boolean memSet = false;
46 boolean dockIcon = false;
47 boolean dockName = false;
48 ARG: for (int i = 0; i < command.size(); i++)
50 String arg = command.get(i);
51 if (arg.startsWith("-Xmx"))
56 else if (arg.startsWith("-Xdock:icon"))
60 else if (arg.startsWith("-Xdock:name"))
69 int percent = maxHeapSizePerCent;
70 String jvmmempc = System.getProperty(maxHeapSizePerCentProperty);
75 int trypercent = Integer.parseInt(jvmmempc);
76 if (0 < trypercent && trypercent <= 100)
82 System.out.println("Property '" + maxHeapSizePerCentProperty
83 + "' should be in range 1..100");
88 System.out.println("Error parsing " + maxHeapSizePerCentProperty
89 + " '" + jvmmempc + "'");
94 maxMemLong = MemorySetting.memPercent(percent);
105 memSetting = "-Xmx" + Long.toString(maxMemLong);
106 command.add(memSetting);
114 command.add("-Xdock:icon=" + dockIconPath);
118 // -Xdock:name=... doesn't actually work :(
119 // Leaving it in in case it gets fixed
120 command.add("-Xdock:name=" + "Jalview");
124 command.add(startClass);
125 command.addAll(arguments);
127 final ProcessBuilder builder = new ProcessBuilder(command);
129 // System.out.println("COMMAND: " + String.join(" ", builder.command()));
130 System.out.println("Running " + startClass + " with "
131 + (memSetting == null ? "no memSetting" : memSetting));
136 Process process = builder.start();
138 } catch (IOException e)
140 if (e.getMessage().toLowerCase().contains("memory"))
142 System.out.println("Caught a memory exception: " + e.getMessage());
143 // Probably the "Cannot allocate memory" error, try without the memory setting
144 ArrayList<String> commandNoMem = new ArrayList<>();
145 for (int i = 0; i < command.size(); i++)
147 if (!command.get(i).startsWith("-Xmx"))
149 commandNoMem.add(command.get(i));
152 final ProcessBuilder builderNoMem = new ProcessBuilder(
154 System.out.println("NO MEM COMMAND: "
155 + String.join(" ", builderNoMem.command()));
158 builderNoMem.inheritIO();
159 Process processNoMem = builderNoMem.start();
160 processNoMem.waitFor();
161 } catch (Exception ex)
163 ex.printStackTrace();
170 } catch (Exception e)