gpt4 book ai didi

java - Applet 类加载器无法在 applet 的 jar 中找到类

转载 作者:搜寻专家 更新时间:2023-11-01 01:48:36 27 4
gpt4 key购买 nike

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


我遇到了一个小程序问题(实际上是一个 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 moreCaused 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>

最佳答案

通过查看 a suggestion 找到了答案发布 related question . Eddie 的回答并没有解决那个特定的问题,但它确实为我提供了解决方案。

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

<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>

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

关于java - Applet 类加载器无法在 applet 的 jar 中找到类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/872905/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com