diff --git a/jcl/src/java.base/share/classes/java/lang/System.java b/jcl/src/java.base/share/classes/java/lang/System.java index 6c938d8d41a..6473d992b07 100644 --- a/jcl/src/java.base/share/classes/java/lang/System.java +++ b/jcl/src/java.base/share/classes/java/lang/System.java @@ -1152,10 +1152,28 @@ 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 */ + } + } } /**