--- /dev/null
+package jalview.bin;
+
+import java.io.File;
+import java.lang.management.ManagementFactory;
+import java.util.ArrayList;
+
+public class Launcher
+{
+
+ private final static String startClass = "jalview.bin.Jalview";
+
+ private final static int maxHeapSizePerCent = 95;
+
+ private final static String maxMemArg = "-maxMem";
+
+ public static void main(String[] args)
+ {
+ final String javaBin = System.getProperty("java.home") + File.separator
+ + "bin" + File.separator + "java";
+
+ ArrayList<String> command = new ArrayList<>();
+ command.add(javaBin);
+
+ for (String jvmArg : ManagementFactory.getRuntimeMXBean()
+ .getInputArguments())
+ {
+ command.add(jvmArg);
+ }
+ command.add("-cp");
+ command.add(ManagementFactory.getRuntimeMXBean().getClassPath());
+ ArrayList<String> arguments = new ArrayList<>();
+ boolean maxMem = false;
+ for (String arg : args)
+ {
+ if (arg.equals(maxMemArg) && !maxMem)
+ {
+ maxMem = true;
+ // remove any existing max heap size argument
+ ARG: for (int i = 0; i < command.size(); i++)
+ {
+ if (command.get(i).startsWith("-Xmx"))
+ {
+ command.remove(i);
+ break ARG; // assuming at most one -Xmx
+ }
+ }
+ long maxMemLong = -1;
+ long physicalMem = getPhysicalMemory();
+ if (physicalMem > 0)
+ {
+ maxMemLong = physicalMem * maxHeapSizePerCent / 100;
+ }
+ if (maxMemLong > 0)
+ {
+ command.add("-Xmx" + Long.toString(maxMemLong));
+ }
+ }
+ else
+ {
+ arguments.add(arg);
+ }
+ }
+ command.add(startClass);
+ command.addAll(arguments);
+
+ final ProcessBuilder builder = new ProcessBuilder(command);
+
+ System.out.println("COMMAND: " + builder.command().toString());
+
+ try
+ {
+ builder.inheritIO();
+ builder.start();
+ } catch (Exception e)
+ {
+ e.printStackTrace();
+ }
+ System.exit(0);
+
+ }
+
+ public static long getPhysicalMemory()
+ {
+ final Object o = ManagementFactory.getOperatingSystemMXBean();
+
+ try
+ {
+ if (o instanceof com.sun.management.OperatingSystemMXBean)
+ {
+ final com.sun.management.OperatingSystemMXBean osb = (com.sun.management.OperatingSystemMXBean) o;
+ return osb.getTotalPhysicalMemorySize();
+ }
+ } catch (NoClassDefFoundError e)
+ {
+ // com.sun.management.OperatingSystemMXBean doesn't exist in this JVM
+ System.out.println("No com.sun.management.OperatingSystemMXBean");
+ }
+
+ // We didn't get a com.sun.management.OperatingSystemMXBean.
+ return -1;
+ }
+
+}