JAL-3691 automatic insertion of Locale.ROOT to toUpperCase() and toLowerCase() and...
[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.util.Locale;
24
25 import java.io.File;
26 import java.io.IOException;
27 import java.lang.management.ManagementFactory;
28 import java.util.ArrayList;
29 import java.util.List;
30
31 import jalview.util.ChannelProperties;
32
33 /**
34  * A Launcher class for Jalview. This class is used to launch Jalview from the
35  * shadowJar when Getdown is not used or available. It attempts to take all the
36  * command line arguments to pass on to the jalview.bin.Jalview class, but to
37  * insert a -Xmx memory setting to a sensible default, using the -jvmmempc and
38  * -jvmmemmax application arguments if specified. If not specified then system
39  * properties will be looked for by jalview.bin.MemorySetting. If the user has
40  * provided the JVM with a -Xmx setting directly and not set -jvmmempc or
41  * -jvmmemmax then this setting will be used and system properties ignored. If
42  * -Xmx is set as well as -jvmmempc or -jvmmemmax as argument(s) then the -Xmx
43  * argument will NOT be passed on to the main application launch.
44  * 
45  * @author bsoares
46  *
47  */
48 public class Launcher
49 {
50   private final static String startClass = "jalview.bin.Jalview";
51
52   private final static String dockIconPath = ChannelProperties
53           .getProperty("logo.512");
54
55   /**
56    * main method for jalview.bin.Launcher. This restarts the same JRE's JVM with
57    * the same arguments but with memory adjusted based on extracted -jvmmempc
58    * and -jvmmemmax application arguments. If on a Mac then extra dock:icon and
59    * dock:name arguments are also set.
60    * 
61    * @param args
62    */
63   public static void main(String[] args)
64   {
65     final String javaBin = System.getProperty("java.home") + File.separator
66             + "bin" + File.separator + "java";
67
68     List<String> command = new ArrayList<>();
69     command.add(javaBin);
70
71     String memSetting = null;
72
73     boolean isAMac = System.getProperty("os.name").indexOf("Mac") > -1;
74
75     for (String jvmArg : ManagementFactory.getRuntimeMXBean()
76             .getInputArguments())
77     {
78       command.add(jvmArg);
79     }
80     command.add("-cp");
81     command.add(ManagementFactory.getRuntimeMXBean().getClassPath());
82
83     String jvmmempc = null;
84     String jvmmemmax = null;
85     ArrayList<String> arguments = new ArrayList<>();
86     for (String arg : args)
87     {
88       // jvmmempc and jvmmemmax args used to set memory and are not passed on to
89       // startClass
90       if (arg.startsWith(
91               "-" + MemorySetting.MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "="))
92       {
93         jvmmempc = arg.substring(
94                 MemorySetting.MAX_HEAPSIZE_PERCENT_PROPERTY_NAME.length()
95                         + 2);
96       }
97       else if (arg.startsWith(
98               "-" + MemorySetting.MAX_HEAPSIZE_PROPERTY_NAME + "="))
99       {
100         jvmmemmax = arg.substring(
101                 MemorySetting.MAX_HEAPSIZE_PROPERTY_NAME.length() + 2);
102       }
103       else
104       {
105         arguments.add(arg);
106       }
107     }
108
109     // add memory setting if not specified
110     boolean memSet = false;
111     boolean dockIcon = false;
112     boolean dockName = false;
113     for (int i = 0; i < command.size(); i++)
114     {
115       String arg = command.get(i);
116       if (arg.startsWith("-Xmx"))
117       {
118         // only use -Xmx if jvmmemmax and jvmmempc have not been set
119         if (jvmmempc == null && jvmmemmax == null)
120         {
121           memSetting = arg;
122           memSet = true;
123         }
124       }
125       else if (arg.startsWith("-Xdock:icon"))
126       {
127         dockIcon = true;
128       }
129       else if (arg.startsWith("-Xdock:name"))
130       {
131         dockName = true;
132       }
133     }
134
135     if (!memSet)
136     {
137       long maxMemLong = MemorySetting.getMemorySetting(jvmmemmax, jvmmempc);
138
139       if (maxMemLong > 0)
140       {
141         memSetting = "-Xmx" + Long.toString(maxMemLong);
142         memSet = true;
143         command.add(memSetting);
144       }
145     }
146
147     if (isAMac)
148     {
149       if (!dockIcon)
150       {
151         command.add("-Xdock:icon=" + dockIconPath);
152       }
153       if (!dockName)
154       {
155         // -Xdock:name=... doesn't actually work :(
156         // Leaving it in in case it gets fixed
157         command.add(
158                 "-Xdock:name=" + ChannelProperties.getProperty("app_name"));
159       }
160     }
161
162     String scalePropertyArg = HiDPISetting.getScalePropertyArg();
163     if (scalePropertyArg != null)
164     {
165       System.out.println("Running " + startClass + " with scale setting "
166               + scalePropertyArg);
167       command.add(scalePropertyArg);
168     }
169
170     command.add(startClass);
171     command.addAll(arguments);
172
173     final ProcessBuilder builder = new ProcessBuilder(command);
174
175     if (Boolean.parseBoolean(System.getProperty("launcherprint", "false")))
176     {
177       System.out.println(
178               "LAUNCHER COMMAND: " + String.join(" ", builder.command()));
179     }
180     System.out.println("Running " + startClass + " with "
181             + (memSetting == null ? "no memory setting"
182                     : ("memory setting " + memSetting)));
183
184     if (Boolean.parseBoolean(System.getProperty("launcherstop", "false")))
185     {
186       System.exit(0);
187     }
188     try
189     {
190       builder.inheritIO();
191       Process process = builder.start();
192       process.waitFor();
193     } catch (IOException e)
194     {
195       if (e.getMessage().toLowerCase(Locale.ROOT).contains("memory"))
196       {
197         System.out.println("Caught a memory exception: " + e.getMessage());
198         // Probably the "Cannot allocate memory" error, try without the memory
199         // setting
200         ArrayList<String> commandNoMem = new ArrayList<>();
201         for (int i = 0; i < command.size(); i++)
202         {
203           if (!command.get(i).startsWith("-Xmx"))
204           {
205             commandNoMem.add(command.get(i));
206           }
207         }
208         final ProcessBuilder builderNoMem = new ProcessBuilder(
209                 commandNoMem);
210         System.out.println("Command without memory setting: "
211                 + String.join(" ", builderNoMem.command()));
212         try
213         {
214           builderNoMem.inheritIO();
215           Process processNoMem = builderNoMem.start();
216           processNoMem.waitFor();
217         } catch (Exception ex)
218         {
219           ex.printStackTrace();
220         }
221       }
222       else
223       {
224         e.printStackTrace();
225       }
226     } catch (Exception e)
227     {
228       e.printStackTrace();
229     }
230     // System.exit(0);
231
232   }
233
234 }