@@ -44,20 +44,27 @@ char* findLastPathComponent(char *buffer, const char *comp) {
4444/*
4545 * Removes the trailing file name and any intermediate platform
4646 * directories, if any, and its enclosing directory.
47+ * Second parameter is a hint about the type of a file. JNI_TRUE is for
48+ * shared libraries and JNI_FALSE is for executables.
4749 * Ex: if a buffer contains "/foo/bin/javac" or "/foo/bin/x64/javac", the
4850 * truncated resulting buffer will contain "/foo".
4951 */
5052jboolean
51- TruncatePath (char * buf )
53+ TruncatePath (char * buf , jboolean pathisdll )
5254{
53- // try bin directory, maybe an executable
54- char * p = findLastPathComponent (buf , "/bin/" );
55+ /*
56+ * If the file is a library, try lib directory first and then bin
57+ * directory.
58+ * If the file is an executable, try bin directory first and then lib
59+ * directory.
60+ */
61+
62+ char * p = findLastPathComponent (buf , pathisdll ? "/lib/" : "/bin/" );
5563 if (p != NULL ) {
5664 * p = '\0' ;
5765 return JNI_TRUE ;
5866 }
59- // try lib directory, maybe a library
60- p = findLastPathComponent (buf , "/lib/" );
67+ p = findLastPathComponent (buf , pathisdll ? "/bin/" : "/lib/" );
6168 if (p != NULL ) {
6269 * p = '\0' ;
6370 return JNI_TRUE ;
@@ -79,7 +86,7 @@ GetApplicationHome(char *buf, jint bufsize)
7986 } else {
8087 return JNI_FALSE ;
8188 }
82- return TruncatePath (buf );
89+ return TruncatePath (buf , JNI_FALSE );
8390}
8491
8592/*
@@ -94,7 +101,7 @@ GetApplicationHomeFromDll(char *buf, jint bufsize)
94101 if (dladdr ((void * )& GetApplicationHomeFromDll , & info ) != 0 ) {
95102 char * path = realpath (info .dli_fname , buf );
96103 if (path == buf ) {
97- return TruncatePath (buf );
104+ return TruncatePath (buf , JNI_TRUE );
98105 }
99106 }
100107 return JNI_FALSE ;
0 commit comments