Skip to content

Commit

Permalink
Merge pull request #20295 from JasonFengJ9/nlcerror-0.48
Browse files Browse the repository at this point in the history
(0.48) Attempt to reload the library in case of UnsatisfiedLinkError
  • Loading branch information
pshipton authored Oct 4, 2024
2 parents 591c050 + 1210fc9 commit 8899b66
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions jcl/src/java.base/share/classes/java/lang/System.java
Original file line number Diff line number Diff line change
Expand Up @@ -1142,10 +1142,30 @@ public static void loadLibrary(String libName) {
smngr.checkLink(libName);
}
/*[IF JAVA_SPEC_VERSION >= 15]*/
ClassLoader.loadLibrary(getCallerClass(), libName);
Class<?> callerClass = getCallerClass();
/*[ELSE]*/
ClassLoader.loadLibraryWithClassLoader(libName, ClassLoader.callerClassLoader());
ClassLoader callerClassLoader = ClassLoader.callerClassLoader();
/*[ENDIF] JAVA_SPEC_VERSION >= 15 */
try {
/*[IF JAVA_SPEC_VERSION >= 15]*/
ClassLoader.loadLibrary(callerClass, libName);
/*[ELSE]*/
ClassLoader.loadLibraryWithClassLoader(libName, callerClassLoader);
/*[ENDIF] JAVA_SPEC_VERSION >= 15 */
} catch (UnsatisfiedLinkError ule) {
String errorMessage = ule.getMessage();
if ((errorMessage != null) && errorMessage.contains("already loaded in another classloader")) { //$NON-NLS-1$
// attempt to unload the classloader, and retry
gc();
/*[IF JAVA_SPEC_VERSION >= 15]*/
ClassLoader.loadLibrary(callerClass, libName);
/*[ELSE]*/
ClassLoader.loadLibraryWithClassLoader(libName, callerClassLoader);
/*[ENDIF] JAVA_SPEC_VERSION >= 15 */
} else {
throw ule;
}
}
}

/**
Expand Down

0 comments on commit 8899b66

Please sign in to comment.