JAL-3609 removed debugging prints
[jalview.git] / src / jalview / bin / Launcher.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.bin;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.lang.management.ManagementFactory;
26 import java.util.ArrayList;
27 import java.util.List;
28
29 /**
30  * A Launcher class for Jalview. This class is used to launch Jalview from the
31  * shadowJar when Getdown is not used or available. It attempts to take all the
32  * command line arguments to pass on to the jalview.bin.Jalview class, but to
33  * insert a -Xmx memory setting to a sensible default, using the -jvmmempc and
34  * -jvmmemmax application arguments if specified. If not specified then system
35  * properties will be looked for by jalview.bin.MemorySetting. If the user has
36  * provided the JVM with a -Xmx setting directly and not set -jvmmempc or
37  * -jvmmemmax then this setting will be used and system properties ignored. If
38  * -Xmx is set as well as -jvmmempc or -jvmmemmax as argument(s) then the -Xmx
39  * argument will NOT be passed on to the main application launch.
40  * 
41  * @author bsoares
42  *
43  */
44 public class Launcher
45 {
46   private final static String startClass = "jalview.bin.Jalview";
47
48   private final static String dockIconPath = "JalviewLogo_Huge.png";
49
50   /**
51    * main method for jalview.bin.Launcher. This restarts the same JRE's JVM with
52    * the same arguments but with memory adjusted based on extracted -jvmmempc
53    * and -jvmmemmax application arguments. If on a Mac then extra dock:icon and
54    * dock:name arguments are also set.
55    * 
56    * @param args
57    */
58   public static void main(String[] args)
59   {
60     final String javaBin = System.getProperty("java.home") + File.separator
61             + "bin" + File.separator + "java";
62
63     List<String> command = new ArrayList<>();
64     command.add(javaBin);
65
66     String memSetting = null;
67
68     boolean isAMac = System.getProperty("os.name").indexOf("Mac") > -1;
69
70     for (String jvmArg : ManagementFactory.getRuntimeMXBean()
71             .getInputArguments())
72     {
73       command.add(jvmArg);
74     }
75     command.add("-cp");
76     command.add(ManagementFactory.getRuntimeMXBean().getClassPath());
77
78     String jvmmempc = null;
79     String jvmmemmax = null;
80     ArrayList<String> arguments = new ArrayList<>();
81     for (String arg : args)
82     {
83       // jvmmempc and jvmmemmax args used to set memory and are not passed on to
84       // startClass
85       if (arg.startsWith(
86               "-" + MemorySetting.MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "="))
87       {
88         jvmmempc = arg.substring(
89                 MemorySetting.MAX_HEAPSIZE_PERCENT_PROPERTY_NAME.length()
90                         + 2);
91       }
92       else if (arg.startsWith(
93               "-" + MemorySetting.MAX_HEAPSIZE_PROPERTY_NAME + "="))
94       {
95         jvmmemmax = arg.substring(
96                 MemorySetting.MAX_HEAPSIZE_PROPERTY_NAME.length() + 2);
97       }
98       else
99       {
100         arguments.add(arg);
101       }
102     }
103
104     // add memory setting if not specified
105     boolean memSet = false;
106     boolean dockIcon = false;
107     boolean dockName = false;
108     for (int i = 0; i < command.size(); i++)
109     {
110       String arg = command.get(i);
111       if (arg.startsWith("-Xmx"))
112       {
113         // only use -Xmx if jvmmemmax and jvmmempc have not been set
114         if (jvmmempc == null && jvmmemmax == null)
115         {
116           memSetting = arg;
117           memSet = true;
118         }
119       }
120       else if (arg.startsWith("-Xdock:icon"))
121       {
122         dockIcon = true;
123       }
124       else if (arg.startsWith("-Xdock:name"))
125       {
126         dockName = true;
127       }
128     }
129
130     if (!memSet)
131     {
132       long maxMemLong = MemorySetting.getMemorySetting(jvmmemmax, jvmmempc);
133
134       if (maxMemLong > 0)
135       {
136         memSetting = "-Xmx" + Long.toString(maxMemLong);
137         memSet = true;
138         command.add(memSetting);
139       }
140     }
141
142     if (isAMac)
143     {
144       if (!dockIcon)
145       {
146         command.add("-Xdock:icon=" + dockIconPath);
147       }
148       if (!dockName)
149       {
150         // -Xdock:name=... doesn't actually work :(
151         // Leaving it in in case it gets fixed
152         command.add("-Xdock:name=" + "Jalview");
153       }
154     }
155
156     String scalePropertyArg = HiDPISetting.getScalePropertyArg();
157     if (scalePropertyArg != null)
158     {
159       command.add(scalePropertyArg);
160     }
161
162     command.add(startClass);
163     command.addAll(arguments);
164
165     final ProcessBuilder builder = new ProcessBuilder(command);
166
167     // System.out.println("COMMAND: " + String.join(" ", builder.command()));
168     System.out.println("Running " + startClass + " with "
169             + (memSetting == null ? "no memory setting" : memSetting));
170
171     if (Boolean.parseBoolean(System.getProperty("launcherstop")))
172     {
173       System.exit(0);
174     }
175     try
176     {
177       builder.inheritIO();
178       Process process = builder.start();
179       process.waitFor();
180     } catch (IOException e)
181     {
182       if (e.getMessage().toLowerCase().contains("memory"))
183       {
184         System.out.println("Caught a memory exception: " + e.getMessage());
185         // Probably the "Cannot allocate memory" error, try without the memory
186         // setting
187         ArrayList<String> commandNoMem = new ArrayList<>();
188         for (int i = 0; i < command.size(); i++)
189         {
190           if (!command.get(i).startsWith("-Xmx"))
191           {
192             commandNoMem.add(command.get(i));
193           }
194         }
195         final ProcessBuilder builderNoMem = new ProcessBuilder(
196                 commandNoMem);
197         System.out.println("Command without memory setting: "
198                 + String.join(" ", builderNoMem.command()));
199         try
200         {
201           builderNoMem.inheritIO();
202           Process processNoMem = builderNoMem.start();
203           processNoMem.waitFor();
204         } catch (Exception ex)
205         {
206           ex.printStackTrace();
207         }
208       }
209       else
210       {
211         e.printStackTrace();
212       }
213     } catch (Exception e)
214     {
215       e.printStackTrace();
216     }
217     // System.exit(0);
218
219   }
220
221 }