package jalview.bin;
import java.io.File;
+import java.io.IOException;
import java.lang.management.ManagementFactory;
-import java.lang.management.OperatingSystemMXBean;
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 dockIconPath = "JalviewLogo_Huge.png";
public static void main(String[] args)
ArrayList<String> command = new ArrayList<>();
command.add(javaBin);
+ String memSetting = null;
+
boolean isAMac = System.getProperty("os.name").indexOf("Mac") > -1;
for (String jvmArg : ManagementFactory.getRuntimeMXBean()
// add memory setting if not specified
boolean memSet = false;
boolean dockIcon = false;
+ boolean dockName = false;
ARG: for (int i = 0; i < command.size(); i++)
{
String arg = command.get(i);
if (arg.startsWith("-Xmx"))
{
+ memSetting = arg;
memSet = true;
}
else if (arg.startsWith("-Xdock:icon"))
{
dockIcon = true;
}
+ else if (arg.startsWith("-Xdock:name"))
+ {
+ dockName = true;
+ }
}
if (!memSet)
{
- long maxMemLong = -1;
- long physicalMem = getPhysicalMemory();
- if (physicalMem > 0)
- {
- maxMemLong = physicalMem * maxHeapSizePerCent / 100;
- }
+ long maxMemLong = MemorySetting.getMemorySetting();
+
if (maxMemLong > 0)
{
- command.add("-Xmx" + Long.toString(maxMemLong));
+ memSetting = "-Xmx" + Long.toString(maxMemLong);
+ command.add(memSetting);
}
}
- if (!dockIcon)
+ if (isAMac)
{
- command.add("-Xdock:icon=" + dockIconPath);
- // -Xdock:name=... doesn't actually work :(
- // Leaving it in in case it gets fixed
- command.add("-Xdock:name=" + "Jalview");
+ if (!dockIcon)
+ {
+ command.add("-Xdock:icon=" + dockIconPath);
+ }
+ if (!dockName)
+ {
+ // -Xdock:name=... doesn't actually work :(
+ // Leaving it in in case it gets fixed
+ command.add("-Xdock:name=" + "Jalview");
+ }
}
command.add(startClass);
final ProcessBuilder builder = new ProcessBuilder(command);
- System.out.println("COMMAND: " + String.join(" ", builder.command()));
+ // System.out.println("COMMAND: " + String.join(" ", builder.command()));
+ System.out.println("Running " + startClass + " with "
+ + (memSetting == null ? "no memSetting" : memSetting));
- try
- {
- builder.inheritIO();
- builder.start();
- } catch (Exception e)
+ if (System.getProperty("launcherstop") != null
+ && System.getProperty("launcherstop").equals("true"))
{
- e.printStackTrace();
+ System.exit(0);
}
- // System.exit(0);
-
- }
-
- public static long getPhysicalMemory()
- {
- final OperatingSystemMXBean o = ManagementFactory
- .getOperatingSystemMXBean();
-
try
{
- if (o instanceof com.sun.management.OperatingSystemMXBean)
+ builder.inheritIO();
+ Process process = builder.start();
+ process.waitFor();
+ } catch (IOException e)
+ {
+ if (e.getMessage().toLowerCase().contains("memory"))
+ {
+ System.out.println("Caught a memory exception: " + e.getMessage());
+ // Probably the "Cannot allocate memory" error, try without the memory setting
+ ArrayList<String> commandNoMem = new ArrayList<>();
+ for (int i = 0; i < command.size(); i++)
+ {
+ if (!command.get(i).startsWith("-Xmx"))
+ {
+ commandNoMem.add(command.get(i));
+ }
+ }
+ final ProcessBuilder builderNoMem = new ProcessBuilder(
+ commandNoMem);
+ System.out.println("NO MEM COMMAND: "
+ + String.join(" ", builderNoMem.command()));
+ try
+ {
+ builderNoMem.inheritIO();
+ Process processNoMem = builderNoMem.start();
+ processNoMem.waitFor();
+ } catch (Exception ex)
+ {
+ ex.printStackTrace();
+ }
+ }
+ else
{
- final com.sun.management.OperatingSystemMXBean osb = (com.sun.management.OperatingSystemMXBean) o;
- return osb.getTotalPhysicalMemorySize();
+ e.printStackTrace();
}
- } catch (NoClassDefFoundError e)
+ } catch (Exception e)
{
- // com.sun.management.OperatingSystemMXBean doesn't exist in this JVM
- System.out.println("No com.sun.management.OperatingSystemMXBean");
+ e.printStackTrace();
}
+ // System.exit(0);
- // We didn't get a com.sun.management.OperatingSystemMXBean.
- return -1;
}
}