卸载本机库

时间:2015-11-05 21:43:54

标签: java native j2v8

TLDR;

有没有办法告诉java UN加载本机库,以便我可以重新加载它? (我知道它只能加载到一个类加载器中。见下文)。我知道当类加载器被垃圾收集时它被卸载了,但是我没有成功地强制进行垃圾收集。有什么想法吗?

我发现这个问题可以解释我遇到错误的原因: .dll already loaded in another classloader?

但它并不是我想要做的事情。我们有一个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

这是我的方法,它创建了一个新的类加载器来动态加载我的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");
    }

我的问题:有没有办法告诉Java应该卸载本机库?

0 个答案:

没有答案