2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
24 import java.io.FileInputStream;
25 import java.io.FileNotFoundException;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.net.MalformedURLException;
30 import java.util.Properties;
32 import jalview.bin.Console;
34 public class LaunchUtils
37 // setting these is LaunchUtils so don't need to import Platform
38 public final static boolean isMac = System.getProperty("os.name")
41 public final static boolean isWindows = System.getProperty("os.name")
44 private static boolean isJS = /** @j2sNative true || */
47 public static void loadChannelProps(File dir)
49 ChannelProperties.loadProps(dir);
52 private static Properties userPreferences = null;
54 public static String getUserPreference(String key)
56 if (userPreferences == null)
58 String channelPrefsFilename = ChannelProperties
59 .getProperty("preferences.filename");
60 if (channelPrefsFilename == null)
64 File propertiesFile = new File(System.getProperty("user.home"),
65 channelPrefsFilename);
66 if (!propertiesFile.exists())
72 userPreferences = new Properties();
73 userPreferences.load(new FileInputStream(propertiesFile));
74 } catch (FileNotFoundException e)
76 // didn't find user preferences file
78 } catch (IOException e)
80 jalview.bin.Console.errPrintln(e.getMessage());
84 return userPreferences.getProperty(key);
87 public static boolean getBooleanUserPreference(String key)
89 return Boolean.parseBoolean(getUserPreference(key));
92 public static int JAVA_COMPILE_VERSION = 0;
94 public static int getJavaCompileVersion()
100 else if (JAVA_COMPILE_VERSION > 0)
102 return JAVA_COMPILE_VERSION;
104 String buildDetails = "jar:".concat(LaunchUtils.class
105 .getProtectionDomain().getCodeSource().getLocation().toString()
106 .concat("!" + "/.build_properties"));
109 URL localFileURL = new URL(buildDetails);
110 InputStream in = localFileURL.openStream();
111 Properties buildProperties = new Properties();
112 buildProperties.load(in);
114 String JCV = buildProperties.getProperty("JAVA_COMPILE_VERSION",
119 "Could not obtain JAVA_COMPILE_VERSION for comparison");
122 JAVA_COMPILE_VERSION = Integer.parseInt(JCV);
123 } catch (MalformedURLException e)
125 jalview.bin.Console.errPrintln("Could not find " + buildDetails);
127 } catch (IOException e)
129 jalview.bin.Console.errPrintln("Could not load " + buildDetails);
131 } catch (NumberFormatException e)
134 .errPrintln("Could not parse JAVA_COMPILE_VERSION");
138 return JAVA_COMPILE_VERSION;
141 public static int JAVA_VERSION = 0;
143 public static int getJavaVersion()
145 if (LaunchUtils.isJS)
149 else if (JAVA_VERSION > 0)
155 String JV = System.getProperty("java.version");
158 Console.errPrintln("Could not obtain java.version for comparison");
161 if (JV.startsWith("1."))
163 JV = JV.substring(2);
165 JAVA_VERSION = JV.indexOf(".") == -1 ? Integer.parseInt(JV)
166 : Integer.parseInt(JV.substring(0, JV.indexOf(".")));
167 } catch (NumberFormatException e)
169 jalview.bin.Console.errPrintln("Could not parse java.version");
175 public static boolean checkJavaVersion()
177 if (LaunchUtils.isJS)
181 String buildDetails = "jar:".concat(LaunchUtils.class
182 .getProtectionDomain().getCodeSource().getLocation().toString()
183 .concat("!" + "/.build_properties"));
185 int java_compile_version = getJavaCompileVersion();
186 int java_version = getJavaVersion();
188 if (java_compile_version <= 0 || java_version <= 0)
190 Console.errPrintln("Could not make Java version check");
193 // Warn if these java.version and JAVA_COMPILE_VERSION conditions exist
194 // Usually this means a Java 11 compiled JAR being run by a Java 11 JVM
195 if (java_version >= 11 && java_compile_version < 11)
203 public static String findJavaBin(boolean winConsole)
205 return findJavaBin(System.getProperty("java.home"), winConsole, true);
209 * Returns a string path to the most likely java binary wanted to run this
210 * installation of Jalview.
212 * @param winConsole whether to use java.exe (console) in preference to javaw.exe
213 * (only affects Windows).
214 * @param javaHome Try this javaHome dir (defaults to the running java.home).
215 * @param generic Return a generic java command if not found.
217 public static String findJavaBin(String javaHome, boolean winConsole,
220 String javaBin = null;
221 final String javaExe = winConsole ? "java.exe" : "javaw.exe";
222 final String java = "java";
224 if (javaHome != null)
226 // property "channel.app_name" is set by install4j when launching getdown
227 String propertyAppName = System.getProperty("channel.app_name");
228 final String appName = (propertyAppName != null
229 && propertyAppName.length() > 0) ? propertyAppName
230 : ChannelProperties.getProperty("app_name");
232 final String javaBinDir = javaHome + File.separator + "bin"
235 // appName and "Jalview" will not point to javaw.exe or java.exe but in
236 // this case that's okay because the taskbar display name problem doesn't
237 // manifest in Windows. See JAL-3820, JAL-4189.
238 for (String name : new String[] { appName, "Jalview", java, javaExe })
240 if (LaunchUtils.checkJVMSymlink(javaBinDir + name, winConsole))
242 javaBin = javaBinDir + name;
248 if (javaBin == null && generic)
250 javaBin = LaunchUtils.isWindows ? javaExe : java;
257 * checkJVMSymlink returns true if the path in testBin *is* a java binary, or
258 * points to a java binary.
259 * @param testBin The binary or symbolic link to check
260 * @param winConsole whether we are in/want a Windows console (only relevant for Windows,
261 * determines whether we use java.exe or javaw.exe)
263 private static boolean checkJVMSymlink(String testBin, boolean winConsole)
265 File testBinFile = new File(testBin);
266 if (!testBinFile.exists())
270 File targetFile = null;
273 targetFile = testBinFile.getCanonicalFile();
274 } catch (IOException e)
278 final String javaExe = winConsole ? "java.exe" : "javaw.exe";
279 if (targetFile != null && ("java".equals(targetFile.getName())
280 || javaExe.equals(targetFile.getName())))