使用URLClassLoader重新加载jar时出现问题 [英] Problem reloading a jar using URLClassLoader

查看:1105
本文介绍了使用URLClassLoader重新加载jar时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为应用程序的某些部分添加插件功能到现有应用程序。我希望能够在运行时添加一个jar,应用程序应该能够从jar加载一个类而无需重新启动应用程序。到现在为止还挺好。我使用URLClassLoader在线发现了一些样本,它工作正常。

I need to add plugin functionality to an existing application for certain parts of the application. I want to be able to add a jar at runtime and the application should be able to load a class from the jar without restarting the app. So far so good. I found some samples online using URLClassLoader and it works fine.

我还希望能够在更新版本的jar可用时重新加载同一个类。我再次找到了一些示例和实现这一点的关键,据我所知,我需要为每个新加载使用一个新的类加载器实例。

I also wanted the ability to reload the same class when an updated version of the jar is available. I again found some samples and the key to achieving this as I understand is that I need to use a new classloader instance for each new load.

我写了一些示例代码但是点击NullPointerException。首先让我告诉你们代码:

I wrote some sample code but hit a NullPointerException. First let me show you guys the code:

package test.misc;

import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;

import plugin.misc.IPlugin;

public class TestJarLoading {

    public static void main(String[] args) {

        IPlugin plugin = null;

        while(true) {
            try {
                File file = new File("C:\\plugins\\test.jar");
                String classToLoad = "jartest.TestPlugin";
                URL jarUrl = new URL("jar", "","file:" + file.getAbsolutePath()+"!/");
                URLClassLoader cl = new URLClassLoader(new URL[] {jarUrl}, TestJarLoading.class.getClassLoader());
                Class loadedClass = cl.loadClass(classToLoad);
                plugin = (IPlugin) loadedClass.newInstance();
                plugin.doProc();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    Thread.sleep(30000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

IPlugin是一个简单的接口,只有一个方法doProc:

IPlugin is a simple interface with just one method doProc:

public interface IPlugin {
    void doProc();
}

和jartest.TestPlugin是这个界面的一个实现,其中doProc只打印出一些语句。

and jartest.TestPlugin is an implementation of this interface where doProc just prints out some statements.

现在,我将jartest.TestPlugin类打包到一个名为test.jar的jar中,并将其放在C:\ plugin下并运行此代码。第一次迭代运行顺利,类加载没有问题。

Now, I package the jartest.TestPlugin class into a jar called test.jar and place it under C:\plugins and run this code. The first iteration runs smoothly and the class loads without issues.

当程序执行sleep语句时,我用一个包含同一类的更新版本的新jar替换C:\plugins \ test.jar并等待对于下一次迭代。现在这是我不明白的地方。有时更新的类会被重新加载而没有问题,即下一次迭代运行正常。但有时,我看到抛出一个异常:

When the program is executing the sleep statement, I replace C:\plugins\test.jar with a new jar containing an updated version of the same class and wait for the next iteration of while. Now here's what I don't understand. Sometimes the updated class gets reloaded without issues i.e. the next iteration runs fine. But sometimes, I see an exception thrown:

java.lang.NullPointerException
at java.io.FilterInputStream.close(FilterInputStream.java:155)
at sun.net.www.protocol.jar.JarURLConnection$JarURLInputStream.close(JarURLConnection.java:90)
at sun.misc.Resource.getBytes(Resource.java:137)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:256)
at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at test.misc.TestJarLoading.main(TestJarLoading.java:22)

我在网上搜索并划伤了我的头但是无法真正到达关于为什么这个例外是什么的任何结论wn也是如此 - 只是有时,并非总是如此。

I have searched on the net and scratched my head but can't really arrive at any conclusion as to why this exception is thrown and that too - only sometimes, not always.

我需要你的经验和专业知识来理解这一点。这段代码出了什么问题?请帮助!!

I need your experience and expertise to understand this. What's wrong with this code? Please help!!

如果您需要更多信息,请告诉我。谢谢你的期待!

Let me know if you need any more info. Thanks for looking!

推荐答案

此行为与 bug

2个变通方法这里

这篇关于使用URLClassLoader重新加载jar时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆