changeset 13460:c43113f67573

8145409: Better detect JRE that JLI will be using Reviewed-by: ksrini
author dcherepanov
date Thu, 14 Jan 2016 18:22:32 +0300
parents f5d02fbd8095
children 41d23e89d583
files src/java.base/windows/native/libjli/java_md.c src/java.base/windows/native/libjli/java_md.h
diffstat 2 files changed, 46 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/windows/native/libjli/java_md.c	Thu Jan 14 13:53:13 2016 +0100
+++ b/src/java.base/windows/native/libjli/java_md.c	Thu Jan 14 18:22:32 2016 +0300
@@ -337,6 +337,15 @@
         }
     }
 
+    /* Try getting path to JRE from path to JLI.DLL */
+    if (GetApplicationHomeFromDll(path, pathsize)) {
+        JLI_Snprintf(javadll, sizeof(javadll), "%s\\bin\\" JAVA_DLL, path);
+        if (stat(javadll, &s) == 0) {
+            JLI_TraceLauncher("JRE path is %s\n", path);
+            return JNI_TRUE;
+        }
+    }
+
     JLI_ReportErrorMessage(JRE_ERROR8 JAVA_DLL);
     return JNI_FALSE;
 
@@ -404,17 +413,17 @@
 }
 
 /*
- * If app is "c:\foo\bin\javac", then put "c:\foo" into buf.
+ * Removes the trailing file name and one sub-folder from a path.
+ * If buf is "c:\foo\bin\javac", then put "c:\foo" into buf.
  */
 jboolean
-GetApplicationHome(char *buf, jint bufsize)
+TruncatePath(char *buf)
 {
     char *cp;
-    GetModuleFileName(0, buf, bufsize);
     *JLI_StrRChr(buf, '\\') = '\0'; /* remove .exe file name */
     if ((cp = JLI_StrRChr(buf, '\\')) == 0) {
         /* This happens if the application is in a drive root, and
-         * there is no bin directory. */
+        * there is no bin directory. */
         buf[0] = '\0';
         return JNI_FALSE;
     }
@@ -423,6 +432,36 @@
 }
 
 /*
+ * Retrieves the path to the JRE home by locating the executable file
+ * of the current process and then truncating the path to the executable
+ */
+jboolean
+GetApplicationHome(char *buf, jint bufsize)
+{
+    GetModuleFileName(NULL, buf, bufsize);
+    return TruncatePath(buf);
+}
+
+/*
+ * Retrieves the path to the JRE home by locating JLI.DLL and
+ * then truncating the path to JLI.DLL
+ */
+jboolean
+GetApplicationHomeFromDll(char *buf, jint bufsize)
+{
+    HMODULE hModule;
+    DWORD dwFlags =
+        GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
+        GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT;
+
+    if (GetModuleHandleEx(dwFlags, (LPCSTR)&GetJREPath, &hModule) == 0) {
+        return JNI_FALSE;
+    };
+    GetModuleFileName(hModule, buf, bufsize);
+    return TruncatePath(buf);
+}
+
+/*
  * Support for doing cheap, accurate interval timing.
  */
 static jboolean counterAvailable = JNI_FALSE;
--- a/src/java.base/windows/native/libjli/java_md.h	Thu Jan 14 13:53:13 2016 +0100
+++ b/src/java.base/windows/native/libjli/java_md.h	Thu Jan 14 18:22:32 2016 +0300
@@ -54,4 +54,7 @@
 
 int UnsetEnv(char *name);
 
+jboolean
+GetApplicationHomeFromDll(char *buf, jint bufsize);
+
 #endif /* JAVA_MD_H */