小程序类加载器在小程序的 jar 中找不到类 [英] Applet class loader cannot find a class in the applet's jar

查看:26
本文介绍了小程序类加载器在小程序的 jar 中找不到类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始问这个问题,然后在提交之前想出了答案.无论如何,我决定发布这个问题,以便其他遇到同样问题的人能够从我的错误中吸取教训.

<小时>

我的小程序有问题(a JApplet 实际上) 无法实例化与小程序包含在同一个 jar 中的另一个类.我在 Java 控制台上看到的异常是:

<前>线程线程 applet-com.company.program.cm.hmi.MediatorApplet-1"中的异常 java.lang.NoClassDefFoundError: com/company/program/cm/cs/JDataStore在 com.company.program.cm.hmi.MediatorApplet.getMediator(MediatorApplet.java:63)在 com.company.program.cm.hmi.MediatorApplet.init(MediatorApplet.java:49)在 sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(未知来源)在 java.lang.Thread.run(未知来源)引起:java.lang.ClassNotFoundException:com.company.program.cm.cs.JDataStore在 sun.plugin2.applet.Applet2ClassLoader.findClass(来源不明)在 java.lang.ClassLoader.loadClass(来源不明)在 java.lang.ClassLoader.loadClass(来源不明)在 java.lang.ClassLoader.loadClassInternal(来源不明)... 4个引起:java.io.IOException:打开HTTP连接失败:http://localhost:8080/TransportHMI/pages/com/company/program/cm/cs/JDataStore.class在 sun.plugin2.applet.Applet2ClassLoader.getBytes(来源不明)在 sun.plugin2.applet.Applet2ClassLoader.access$000(来源不明)在 sun.plugin2.applet.Applet2ClassLoader$1.run(来源不明)在 java.security.AccessController.doPrivileged(Native Method)... 8个

我知道 JDataStore 类包含在 jar 中.如果我使用 jar tvf CM_Library.jar 列出内容,我可以看到它在正确的包下.但是,上面的链式异常让我相信类加载器不会在存档中搜索 JDataStore,而是在 Web 服务器上寻找 JDataStore.class 文件.为什么是这样?类加载器知道从 jar 加载 MediatorApplet 类,为什么它也不检查 JDataStore 呢?如果我没有在 applet 标签上正确指定参数,我也会在这里包括:

</小程序>

解决方案

通过查看 针对相关问题.Eddie 的回答并没有解决那个特定的问题,但它确实为我提供了解决方案.

从我的问题中不是特别明显的是 JDataStore 类继承自包含在不同 jar 中的另一个类.我已经几个月没有处理 JDataStore 的实现细节了,所以我完全忘记了它的父类 org.json.simple.JSONObject 不在 CM_Library.jar 中,而是在 json_simple-1.0 中.2.jar.修复相当简单,只需将丢失的 jar 复制到 codebase 目录并将丢失的 jar 添加到小程序标记的 archive 属性中以逗号分隔的存档列表中:

</小程序>

这修复了错误.异常消息不是特别有用.这会让你相信它根本找不到类,而实际问题是它无法为请求的类加载超类.

I started to ask this question and then figured out the answer before submitting it. I've decided to post the question anyway so that other people who run into the same problem will be able to learn from my mistakes.


I'm having a problem with an applet (a JApplet actually) unable to instantiate another class which is included in the same jar as the applet. The exception I'm seeing on the Java console is:

Exception in thread "thread applet-com.company.program.cm.hmi.MediatorApplet-1" java.lang.NoClassDefFoundError: com/company/program/cm/cs/JDataStore
    at com.company.program.cm.hmi.MediatorApplet.getMediator(MediatorApplet.java:63)
    at com.company.program.cm.hmi.MediatorApplet.init(MediatorApplet.java:49)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: com.company.program.cm.cs.JDataStore
    at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClassInternal(Unknown Source)
    ... 4 more
Caused by: java.io.IOException: open HTTP connection failed:http://localhost:8080/TransportHMI/pages/com/company/program/cm/cs/JDataStore.class
    at sun.plugin2.applet.Applet2ClassLoader.getBytes(Unknown Source)
    at sun.plugin2.applet.Applet2ClassLoader.access$000(Unknown Source)
    at sun.plugin2.applet.Applet2ClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    ... 8 more

I know that the JDataStore class is included in the jar. If I list the contents using jar tvf CM_Library.jar I can see that it is there under the proper package. However, the chained exceptions above lead me to believe that the class loader isn't searching the archive for JDataStore, and is looking for the JDataStore.class file on the web server instead. Why is this? The class loader knows to load the MediatorApplet class from the jar, why doesn't it check it for JDataStore as well? In case I haven't specified the parameters correctly on the applet tag, I'll include that here as well:

<applet id="mediator-applet"
        width="0"
        height="0"
        codebase="./"
        archive="CM_Library.jar"
        code="com.company.program.cm.hmi.MediatorApplet">
</applet>

解决方案

Found the answer from looking at a suggestion posted for a related question. Eddie's answer didn't solve that particular problem, but it did give me the solution for mine.

What isn't particularly obvious from my question is that the JDataStore class inherits from another class which is contained in a different jar. I hadn't had to deal with the implementation details of JDataStore in a couple of months, so I completely forgot that its parent class, org.json.simple.JSONObject, wasn't in CM_Library.jar, but was in json_simple-1.0.2.jar. The fix is fairly simple, just copy the missing jar to the codebase directory and add the missing jar to the comma-separated list of archives in the applet tag's archive attribute:

<applet id="mediator-applet"
        width="0"
        height="0"
        codebase="./"
        archive="CM_Library.jar, json_simple-1.0.2.jar"
        code="com.company.program.cm.hmi.MediatorApplet">
</applet>

That fixes the error. The exception message is not particularly helpful. It would lead you to believe that it can't find the class at all, when the actual problem is that it cannot load the super class for the requested class.

这篇关于小程序类加载器在小程序的 jar 中找不到类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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