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 public class LaunchUtils
35 // setting these is LaunchUtils so don't need to import Platform
36 public final static boolean isMac = System.getProperty("os.name")
39 public final static boolean isWindows = System.getProperty("os.name")
42 private static boolean isJS = /** @j2sNative true || */
45 public static void loadChannelProps(File dir)
47 ChannelProperties.loadProps(dir);
50 private static Properties userPreferences = null;
52 public static String getUserPreference(String key)
54 if (userPreferences == null)
56 String channelPrefsFilename = ChannelProperties
57 .getProperty("preferences.filename");
58 if (channelPrefsFilename == null)
62 File propertiesFile = new File(System.getProperty("user.home"),
63 channelPrefsFilename);
64 if (!propertiesFile.exists())
70 userPreferences = new Properties();
71 userPreferences.load(new FileInputStream(propertiesFile));
72 } catch (FileNotFoundException e)
74 // didn't find user preferences file
76 } catch (IOException e)
78 System.err.println(e.getMessage());
82 return userPreferences.getProperty(key);
85 public static boolean getBooleanUserPreference(String key)
87 return Boolean.parseBoolean(getUserPreference(key));
90 public static int JAVA_COMPILE_VERSION = 0;
92 public static int getJavaCompileVersion()
98 else if (JAVA_COMPILE_VERSION > 0)
100 return JAVA_COMPILE_VERSION;
102 String buildDetails = "jar:".concat(LaunchUtils.class
103 .getProtectionDomain().getCodeSource().getLocation().toString()
104 .concat("!" + "/.build_properties"));
107 URL localFileURL = new URL(buildDetails);
108 InputStream in = localFileURL.openStream();
109 Properties buildProperties = new Properties();
110 buildProperties.load(in);
112 String JCV = buildProperties.getProperty("JAVA_COMPILE_VERSION",
117 "Could not obtain JAVA_COMPILE_VERSION for comparison");
120 JAVA_COMPILE_VERSION = Integer.parseInt(JCV);
121 } catch (MalformedURLException e)
123 System.err.println("Could not find " + buildDetails);
125 } catch (IOException e)
127 System.err.println("Could not load " + buildDetails);
129 } catch (NumberFormatException e)
131 System.err.println("Could not parse JAVA_COMPILE_VERSION");
135 return JAVA_COMPILE_VERSION;
138 public static int JAVA_VERSION = 0;
140 public static int getJavaVersion()
142 if (LaunchUtils.isJS)
146 else if (JAVA_VERSION > 0)
152 String JV = System.getProperty("java.version");
155 System.out.println("Could not obtain java.version for comparison");
158 if (JV.startsWith("1."))
160 JV = JV.substring(2);
162 JAVA_VERSION = JV.indexOf(".") == -1 ? Integer.parseInt(JV)
163 : Integer.parseInt(JV.substring(0, JV.indexOf(".")));
164 } catch (NumberFormatException e)
166 System.err.println("Could not parse java.version");
172 public static boolean checkJavaVersion()
174 if (LaunchUtils.isJS)
178 String buildDetails = "jar:".concat(LaunchUtils.class
179 .getProtectionDomain().getCodeSource().getLocation().toString()
180 .concat("!" + "/.build_properties"));
182 int java_compile_version = getJavaCompileVersion();
183 int java_version = getJavaVersion();
185 if (java_compile_version <= 0 || java_version <= 0)
187 System.out.println("Could not make Java version check");
190 // Warn if these java.version and JAVA_COMPILE_VERSION conditions exist
191 // Usually this means a Java 11 compiled JAR being run by a Java 11 JVM
192 if (java_version >= 11 && java_compile_version < 11)
200 public static String findJavaBin(boolean winConsole)
202 return findJavaBin(System.getProperty("java.home"), winConsole, true);
206 * Returns a string path to the most likely java binary wanted to run this
207 * installation of Jalview.
209 * @param winConsole whether to use java.exe (console) in preference to javaw.exe
210 * (only affects Windows).
211 * @param javaHome Try this javaHome dir (defaults to the running java.home).
212 * @param generic Return a generic java command if not found.
214 public static String findJavaBin(String javaHome, boolean winConsole,
217 String javaBin = null;
218 final String javaExe = winConsole ? "java.exe" : "javaw.exe";
219 final String java = "java";
221 if (javaHome != null)
223 // property "channel.app_name" is set by install4j when launching getdown
224 String propertyAppName = System.getProperty("channel.app_name");
225 final String appName = (propertyAppName != null
226 && propertyAppName.length() > 0) ? propertyAppName
227 : ChannelProperties.getProperty("app_name");
229 final String javaBinDir = javaHome + File.separator + "bin"
232 // appName and "Jalview" will not point to javaw.exe or java.exe but in
233 // this case that's okay because the taskbar display name problem doesn't
234 // manifest in Windows. See JAL-3820, JAL-4189.
235 for (String name : new String[] { appName, "Jalview", java, javaExe })
237 if (LaunchUtils.checkJVMSymlink(javaBinDir + name, winConsole))
239 javaBin = javaBinDir + name;
245 if (javaBin == null && generic)
247 javaBin = LaunchUtils.isWindows ? javaExe : java;
254 * checkJVMSymlink returns true if the path in testBin *is* a java binary, or
255 * points to a java binary.
256 * @param testBin The binary or symbolic link to check
257 * @param winConsole whether we are in/want a Windows console (only relevant for Windows,
258 * determines whether we use java.exe or javaw.exe)
260 private static boolean checkJVMSymlink(String testBin, boolean winConsole)
262 File testBinFile = new File(testBin);
263 if (!testBinFile.exists())
267 File targetFile = null;
270 targetFile = testBinFile.getCanonicalFile();
271 } catch (IOException e)
275 final String javaExe = winConsole ? "java.exe" : "javaw.exe";
276 if (targetFile != null && ("java".equals(targetFile.getName())
277 || javaExe.equals(targetFile.getName())))