java 小程序类加载器在小程序的 jar 中找不到类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/872905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Applet class loader cannot find a class in the applet's jar
提问by A. Levy
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 JAppletactually) 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:
我遇到了一个小程序(实际上是JApplet)的问题,无法实例化与小程序包含在同一个 jar 中的另一个类。我在 Java 控制台上看到的异常是:
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<applet id="mediator-applet"
width="0"
height="0"
codebase="./"
archive="CM_Library.jar"
code="com.company.program.cm.hmi.MediatorApplet">
</applet>
0(Unknown Source)
at sun.plugin2.applet.Applet2ClassLoader.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
... 8 more
I know that the JDataStoreclass is included in the jar. If I list the contents using jar tvf CM_Library.jarI 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:
我知道这个JDataStore类包含在 jar 中。如果我使用使用列出内容,jar tvf CM_Library.jar我可以看到它在正确的包下。但是,上面的链式异常让我相信类加载器不是在存档中搜索JDataStore,而是在 Web 服务器上寻找 JDataStore.class 文件。为什么是这样?类加载器知道从 jar 加载 MediatorApplet 类,为什么它也不检查 JDataStore 呢?如果我没有在 applet 标签上正确指定参数,我也会在这里包括:
<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>
回答by A. Levy
Found the answer from looking at a suggestionposted for a related question. Eddie's answer didn't solve that particular problem, but it did give me the solution for mine.
通过查看针对相关问题发布的建议找到了答案。Eddie 的回答并没有解决那个特定的问题,但它确实为我提供了解决方案。
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 archiveattribute:
从我的问题中不是特别明显的是 JDataStore 类继承自包含在不同 jar 中的另一个类。我已经几个月没有处理 JDataStore 的实现细节了,所以我完全忘记了它的父类 org.json.simple.JSONObject 不在 CM_Library.jar 中,而是在 json_simple-1.0 中.2.jar。修复相当简单,只需将丢失的 jar 复制到代码库目录,并将丢失的 jar 添加到小程序标记archive属性中以逗号分隔的存档列表中:
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.
这修复了错误。异常消息不是特别有用。当实际问题是它无法为请求的类加载超类时,它会让您相信它根本找不到类。

