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..929d19440c6 100644 --- a/jcl/src/java.base/share/classes/java/lang/System.java +++ b/jcl/src/java.base/share/classes/java/lang/System.java @@ -1151,11 +1151,24 @@ public static void loadLibrary(String libName) { if (smngr != null) { smngr.checkLink(libName); } + try { +/*[IF JAVA_SPEC_VERSION >= 15]*/ + ClassLoader.loadLibrary(getCallerClass(), libName); +/*[ELSE]*/ + ClassLoader.loadLibraryWithClassLoader(libName, ClassLoader.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(getCallerClass(), libName); + ClassLoader.loadLibrary(getCallerClass(), libName); /*[ELSE]*/ - ClassLoader.loadLibraryWithClassLoader(libName, ClassLoader.callerClassLoader()); + ClassLoader.loadLibraryWithClassLoader(libName, ClassLoader.callerClassLoader()); /*[ENDIF] JAVA_SPEC_VERSION >= 15 */ + } + } } /**