Java加载DLL文件避免Native Library already loaded in another classloader错误
是的,你可以像加载其他库文件一样加载dll文件。不过,在加载dll文件时,可能会出现Native Library already loaded in another classloader错误。为了避免这个错误,你可以使用一个ClassLoader来加载dll文件,并确保只有一个ClassLoader加载它。下面是一个可能的解决方案:\n\njava\nstatic {\n String libraryPath = "D:\syySdfHsm\" ;\n String[] dlls = {"msvcr110", "libeay32", "ssleay32", "sdfapi_x64", "syySdfHsm"} ;\n\n // 创建一个ClassLoader,用于加载dll文件\n URLClassLoader classLoader = new URLClassLoader(new URL[]{new File(libraryPath).toURI().toURL()}) ;\n\n // 加载每个dll文件\n for (String dll : dlls) {\n try {\n System.loadLibrary(dll) ;\n } catch (UnsatisfiedLinkError e) {\n // 如果出现Native Library already loaded in another classloader错误,使用ClassLoader加载dll文件\n try {\n System.load(classLoader.getResource(dll + ".dll").getPath()) ;\n } catch (Exception ex) {\n ex.printStackTrace() ;\n }\n }\n }\n}\n\n\n这段代码使用URLClassLoader来加载dll文件。如果出现Native Library already loaded in another classloader错误,就使用ClassLoader来加载dll文件。请注意,你需要将libraryPath替换为你dll文件所在的路径,并将dlls替换为你要加载的dll文件名(不包括扩展名)。
原文地址: https://www.cveoy.top/t/topic/qqG6 著作权归作者所有。请勿转载和采集!