c91b812e28981b278e16ba17897db0253d3ef733
[jalview.git] / src / jalview / bin / Launcher.java
1 package jalview.bin;
2
3 import java.io.File;
4 import java.lang.management.ManagementFactory;
5 import java.util.ArrayList;
6
7 public class Launcher
8 {
9
10   private final static String startClass = "jalview.bin.Jalview";
11
12   private final static int maxHeapSizePerCent = 95;
13
14   private final static String maxMemArg = "-maxMem";
15
16   public static void main(String[] args)
17   {
18     final String javaBin = System.getProperty("java.home") + File.separator
19             + "bin" + File.separator + "java";
20
21     ArrayList<String> command = new ArrayList<>();
22     command.add(javaBin);
23
24     for (String jvmArg : ManagementFactory.getRuntimeMXBean()
25             .getInputArguments())
26     {
27       command.add(jvmArg);
28     }
29     command.add("-cp");
30     command.add(ManagementFactory.getRuntimeMXBean().getClassPath());
31     ArrayList<String> arguments = new ArrayList<>();
32     boolean maxMem = false;
33     for (String arg : args)
34     {
35       if (arg.equals(maxMemArg) && !maxMem)
36       {
37         maxMem = true;
38         // remove any existing max heap size argument
39         ARG: for (int i = 0; i < command.size(); i++)
40         {
41           if (command.get(i).startsWith("-Xmx"))
42           {
43             command.remove(i);
44             break ARG; // assuming at most one -Xmx
45           }
46         }
47         long maxMemLong = -1;
48         long physicalMem = getPhysicalMemory();
49         if (physicalMem > 0)
50         {
51           maxMemLong = physicalMem * maxHeapSizePerCent / 100;
52         }
53         if (maxMemLong > 0)
54         {
55           command.add("-Xmx" + Long.toString(maxMemLong));
56         }
57       }
58       else
59       {
60         arguments.add(arg);
61       }
62     }
63     command.add(startClass);
64     command.addAll(arguments);
65
66     final ProcessBuilder builder = new ProcessBuilder(command);
67
68     System.out.println("COMMAND: " + builder.command().toString());
69
70     try
71     {
72       builder.inheritIO();
73       builder.start();
74     } catch (Exception e)
75     {
76       e.printStackTrace();
77     }
78     System.exit(0);
79
80   }
81
82   public static long getPhysicalMemory()
83   {
84     final Object o = ManagementFactory.getOperatingSystemMXBean();
85
86     try
87     {
88       if (o instanceof com.sun.management.OperatingSystemMXBean)
89       {
90         final com.sun.management.OperatingSystemMXBean osb = (com.sun.management.OperatingSystemMXBean) o;
91         return osb.getTotalPhysicalMemorySize();
92       }
93     } catch (NoClassDefFoundError e)
94     {
95       // com.sun.management.OperatingSystemMXBean doesn't exist in this JVM
96       System.out.println("No com.sun.management.OperatingSystemMXBean");
97     }
98
99     // We didn't get a com.sun.management.OperatingSystemMXBean.
100     return -1;
101   }
102
103 }