import java.security.MessageDigest;
+import jalview.util.LaunchUtils;
+
import static com.threerings.getdown.Log.log;
/**
return getJVMPath(appdir, false);
}
+ private static String jvmPath = null;
/**
* Reconstructs the path to the JVM used to launch this process.
*
*/
public static String getJVMPath (File appdir, boolean windebug)
{
- // first look in our application directory for an installed VM
- String vmpath = checkJVMPath(new File(appdir, LOCAL_JAVA_DIR).getAbsolutePath(), windebug);
- if (vmpath == null && isMacOS()) {
- vmpath = checkJVMPath(new File(appdir, LOCAL_JAVA_DIR + "/Contents/Home").getAbsolutePath(), windebug);
+ if (jvmPath != null) {
+ return jvmPath;
}
+
+ // first look in our application directory for an installed VM
+ final String appDir = isMacOS() ?
+ (new File(appdir, LOCAL_JAVA_DIR).getAbsolutePath()) + "/Contents/Home"
+ : new File(appdir, LOCAL_JAVA_DIR).getAbsolutePath();
+
+ String javaBin = LaunchUtils.findJavaBin(appDir, windebug, false);
// then fall back to the VM in which we're already running
- if (vmpath == null) {
- vmpath = checkJVMPath(System.getProperty("java.home"), windebug);
+ if (javaBin == null) {
+ javaBin = LaunchUtils.findJavaBin(System.getProperty("java.home"), windebug, false);
}
// then throw up our hands and hope for the best
- if (vmpath == null) {
+ if (javaBin == null) {
+ javaBin = LaunchUtils.findJavaBin(null, windebug, true);
log.warning("Unable to find java [appdir=" + appdir +
", java.home=" + System.getProperty("java.home") + "]!");
- vmpath = "java";
}
// Oddly, the Mac OS X specific java flag -Xdock:name will only work if java is launched
if (isMacOS()) {
try {
File localVM = new File("/usr/bin/java").getCanonicalFile();
- if (localVM.equals(new File(vmpath).getCanonicalFile())) {
- vmpath = "/usr/bin/java";
+ if (localVM.equals(new File(javaBin).getCanonicalFile())) {
+ javaBin = "/usr/bin/java";
}
} catch (IOException ioe) {
log.warning("Failed to check Mac OS canonical VM path.", ioe);
}
}
- return vmpath;
+ jvmPath = javaBin;
+ return jvmPath;
}
private static String _getMD5FileChecksum (File file) {
public class LaunchUtils
{
+ // setting these is LaunchUtils so don't need to import Platform
+ public final static boolean isMac = System.getProperty("os.name")
+ .indexOf("Mac") > -1;
+
+ public final static boolean isWindows = System.getProperty("os.name")
+ .indexOf("Win") > -1;
+
+ private static boolean isJS = /** @j2sNative true || */
+ false;
+
public static void loadChannelProps(File dir)
{
ChannelProperties.loadProps(dir);
public static int getJavaCompileVersion()
{
- if (JAVA_COMPILE_VERSION > 0)
+ if (LaunchUtils.isJS)
+ {
+ return -1;
+ }
+ else if (JAVA_COMPILE_VERSION > 0)
{
return JAVA_COMPILE_VERSION;
}
public static int getJavaVersion()
{
- if (JAVA_VERSION > 0)
+ if (LaunchUtils.isJS)
+ {
+ return -1;
+ }
+ else if (JAVA_VERSION > 0)
{
return JAVA_VERSION;
}
public static boolean checkJavaVersion()
{
+ if (LaunchUtils.isJS)
+ {
+ return true;
+ }
String buildDetails = "jar:".concat(LaunchUtils.class
.getProtectionDomain().getCodeSource().getLocation().toString()
.concat("!" + "/.build_properties"));
return true;
}
+
+ public static String findJavaBin(boolean winConsole)
+ {
+ return findJavaBin(System.getProperty("java.home"), winConsole, true);
+ }
+
+ /*
+ * Returns a string path to the most likely java binary wanted to run this
+ * installation of Jalview.
+ *
+ * @param winConsole whether to use java.exe (console) in preference to javaw.exe
+ * (only affects Windows).
+ * @param javaHome Try this javaHome dir (defaults to the running java.home).
+ * @param generic Return a generic java command if not found.
+ */
+ public static String findJavaBin(String javaHome, boolean winConsole,
+ boolean generic)
+ {
+ String javaBin = null;
+ final String javaExe = winConsole ? "java.exe" : "javaw.exe";
+ final String java = "java";
+
+ if (javaHome != null)
+ {
+ // property "channel.app_name" is set by install4j when launching getdown
+ String propertyAppName = System.getProperty("channel.app_name");
+ final String appName = (propertyAppName != null
+ && propertyAppName.length() > 0) ? propertyAppName
+ : ChannelProperties.getProperty("app_name");
+
+ final String javaBinDir = javaHome + File.separator + "bin"
+ + File.separator;
+
+ // appName and "Jalview" will not point to javaw.exe or java.exe but in
+ // this case that's okay because the taskbar display name problem doesn't
+ // manifest in Windows. See JAL-3820, JAL-4189.
+ for (String name : new String[] { appName, "Jalview", java, javaExe })
+ {
+ if (LaunchUtils.checkJVMSymlink(javaBinDir + name, winConsole))
+ {
+ javaBin = javaBinDir + name;
+ break;
+ }
+ }
+ }
+
+ if (javaBin == null && generic)
+ {
+ javaBin = LaunchUtils.isWindows ? javaExe : java;
+ }
+
+ return javaBin;
+ }
+
+ /*
+ * checkJVMSymlink returns true if the path in testBin *is* a java binary, or
+ * points to a java binary.
+ * @param testBin The binary or symbolic link to check
+ * @param winConsole whether we are in/want a Windows console (only relevant for Windows,
+ * determines whether we use java.exe or javaw.exe)
+ */
+ private static boolean checkJVMSymlink(String testBin, boolean winConsole)
+ {
+ File testBinFile = new File(testBin);
+ if (!testBinFile.exists())
+ {
+ return false;
+ }
+ File targetFile = null;
+ try
+ {
+ targetFile = testBinFile.getCanonicalFile();
+ } catch (IOException e)
+ {
+ return false;
+ }
+ final String javaExe = winConsole ? "java.exe" : "javaw.exe";
+ if (targetFile != null && ("java".equals(targetFile.getName())
+ || javaExe.equals(targetFile.getName())))
+ {
+ return true;
+ }
+ return false;
+ }
}