TLDR;
有什么办法告诉java联合国加载本机库,以便我可以重新加载它?(我知道它只能在单个类加载器中加载。请参见下文)。我知道当类加载器被垃圾回收时,它已被卸载,但是强制垃圾回收没有成功。有任何想法吗?
我发现了这个问题,这解释了为什么我遇到错误: .dll已在另一个类加载器中加载?
但这不是我想做的事。我们有一个jar,希望能够在运行时动态加载(插件样式),以便在另一个服务上运行一些规模测试。
它在第一次运行时运行良好,但是当我停止测试运行并尝试卸载以前的类加载器并加载新的类加载器时,出现以下错误:
Caused by: java.lang.UnsatisfiedLinkError: Could not load J2V8 library. Reasons:
no j2v8_macosx_x86_64 in java.library.path
Native Library /Users/brian.trezise/libj2v8_macosx_x86_64.dylib already loaded in another classloader
at com.eclipsesource.v8.LibraryLoader.loadLibrary(LibraryLoader.java:71)
at com.eclipsesource.v8.V8.load(V8.java:70)
at com.eclipsesource.v8.V8.createV8Runtime(V8.java:132)
... 8 more
Run Code Online (Sandbox Code Playgroud)
这是我的方法,该方法创建一个新的类加载器来动态加载我的jar,如果以前是它,则通过调用使它失效classLoader.close():
public void reloadJar(String jarName) throws IOException {
if (this.classLoader != null) {
classLoader.close(); //Invalidate the old class loader so we can load a new one
}
File file = new File(JAR_FILE_DIR + jarName);
if (!file.exists()) {
throw new IllegalStateException("The specified file does not exist");
}
URL urls[] = {file.toURI().toURL()};
classLoader = new EmbeddedJarClassLoader(urls, Thread.currentThread().getContextClassLoader());
LOGGER.info("Finished loading scale test jar");
}
Run Code Online (Sandbox Code Playgroud)
我的问题:有什么办法告诉Java应该卸载本机库?