Skip to content

Commit 644b708

Browse files
Merge pull request #1422 from matthiasblaesing/jawt_loading_fix
Load jawt library relative to sun.boot.library.path system on unix OSes
2 parents 0774f82 + f57c650 commit 644b708

File tree

2 files changed

+66
-22
lines changed

2 files changed

+66
-22
lines changed

CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Bug Fixes
1717
* [#1411](https://github.com/java-native-access/jna/pull/1411): Do not throw `Win32Exception` on success for empty section in `Kernel32Util#getPrivateProfileSection` - [@mkarg](https://github.com/mkarg).
1818
* [#1414](https://github.com/java-native-access/jna/pull/1414): Fix definition of `c.s.j.p.unix.X11.XK_Shift_R` - [@matthiasblaesing](https://github.com/matthiasblaesing).
1919
* [#1323](https://github.com/java-native-access/jna/issues/1323). Fix crashes in direct callbacks on mac OS aarch64 - [@matthiasblaesing](https://github.com/matthiasblaesing).
20+
* [#1422](https://github.com/java-native-access/jna/pull/1422): Load jawt library relative to `sun.boot.library.path` system on unix OSes - [@matthiasblaesing](https://github.com/matthiasblaesing).
2021

2122
Release 5.10.0
2223
==============

native/dispatch.c

+65-22
Original file line numberDiff line numberDiff line change
@@ -3063,23 +3063,23 @@ Java_com_sun_jna_Native_initIDs(JNIEnv *env, jclass cls) {
30633063
}
30643064

30653065
#ifndef NO_JAWT
3066-
#if !defined(__APPLE__)
3067-
#define JAWT_HEADLESS_HACK
3068-
#ifdef _WIN32
3069-
#define JAWT_NAME "jawt.dll"
3070-
#if defined(_WIN64)
3071-
#define METHOD_NAME "JAWT_GetAWT"
3072-
#else
3073-
#define METHOD_NAME "_JAWT_GetAWT@8"
3074-
#endif
3075-
#else
3076-
#define JAWT_NAME "libjawt.so"
3077-
#define METHOD_NAME "JAWT_GetAWT"
3078-
#endif
3079-
static void* jawt_handle = NULL;
3080-
static jboolean (JNICALL *pJAWT_GetAWT)(JNIEnv*,JAWT*);
3081-
#define JAWT_GetAWT (*pJAWT_GetAWT)
3082-
#endif
3066+
#if !defined(__APPLE__)
3067+
#define JAWT_HEADLESS_HACK
3068+
#ifdef _WIN32
3069+
#if defined(_WIN64)
3070+
#define METHOD_NAME "JAWT_GetAWT"
3071+
#else
3072+
#define METHOD_NAME "_JAWT_GetAWT@8"
3073+
#endif
3074+
#else
3075+
#define METHOD_NAME "JAWT_GetAWT"
3076+
#endif
3077+
3078+
static void* jawt_handle = NULL;
3079+
static jboolean (JNICALL *pJAWT_GetAWT)(JNIEnv*,JAWT*);
3080+
3081+
#define JAWT_GetAWT (*pJAWT_GetAWT)
3082+
#endif
30833083
#endif /* NO_JAWT */
30843084

30853085
JNIEXPORT jlong JNICALL
@@ -3116,17 +3116,60 @@ Java_com_sun_jna_Native_getWindowHandle0(JNIEnv* UNUSED_JAWT(env), jclass UNUSED
31163116
path = (wchar_t*)alloca(len * sizeof(wchar_t));
31173117

31183118
swprintf(path, len, L"%s%s", prop, suffix);
3119-
3119+
31203120
free((void *)prop);
31213121
}
3122-
#undef JAWT_NAME
3123-
#define JAWT_NAME path
3124-
#endif
3125-
if ((jawt_handle = LOAD_LIBRARY(JAWT_NAME, DEFAULT_LOAD_OPTS)) == NULL) {
3122+
if ((jawt_handle = LOAD_LIBRARY(path, DEFAULT_LOAD_OPTS)) == NULL) {
31263123
char msg[MSG_SIZE];
31273124
throwByName(env, EUnsatisfiedLink, LOAD_ERROR(msg, sizeof(msg)));
31283125
return -1;
31293126
}
3127+
#else
3128+
const char* jawtLibraryName = "libjawt.so";
3129+
3130+
// Try to load the libjawt.so library first from the the directories listed
3131+
// in the sun.boot.library.path system property. At least Ubuntu builds the
3132+
// JDK with RUNPATH set instead of RPATH. The two differ in their effect on
3133+
// loading transitive dependencies.
3134+
//
3135+
// RPATHs effect also covers libraries loaded as transitive dependencies,
3136+
// while RUNPATH does not. In the case of JNA libjawt is loaded by
3137+
// libdispatch (the native JNA part), which makes it a transtive load.
3138+
//
3139+
// The solution is to load the library with the full path based on the
3140+
// sun.boot.library.path system property, which points to the native library
3141+
// dirs.
3142+
jstring jprop = get_system_property(env, "sun.boot.library.path");
3143+
if (jprop != NULL) {
3144+
3145+
char* prop = newCString(env, jprop);
3146+
char* saveptr;
3147+
3148+
for(char* propToBeTokeninzed = prop; ; propToBeTokeninzed = NULL) {
3149+
char* pathElement = strtok_r(propToBeTokeninzed, ":", &saveptr);
3150+
3151+
size_t len = strlen(pathElement) + strlen(jawtLibraryName) + 2;
3152+
char* path = (char*) alloca(len);
3153+
3154+
sprintf(path, "%s/%s", pathElement, jawtLibraryName);
3155+
3156+
jawt_handle = LOAD_LIBRARY(path, DEFAULT_LOAD_OPTS);
3157+
if(jawt_handle != NULL || pathElement == NULL) {
3158+
break;
3159+
}
3160+
}
3161+
3162+
free((void *)prop);
3163+
}
3164+
3165+
if (jawt_handle == NULL) {
3166+
if ((jawt_handle = LOAD_LIBRARY(jawtLibraryName, DEFAULT_LOAD_OPTS)) == NULL) {
3167+
char msg[MSG_SIZE];
3168+
throwByName(env, EUnsatisfiedLink, LOAD_ERROR(msg, sizeof(msg)));
3169+
return -1;
3170+
}
3171+
}
3172+
#endif
31303173
if ((pJAWT_GetAWT = (void*)FIND_ENTRY(jawt_handle, METHOD_NAME)) == NULL) {
31313174
char msg[MSG_SIZE], buf[MSG_SIZE - 31 /* literal characters */ - sizeof(METHOD_NAME)];
31323175
snprintf(msg, sizeof(msg), "Error looking up JAWT method %s: %s",

0 commit comments

Comments
 (0)