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