-1

当我尝试运行小程序时出现以下异常:

load: class MyApplet not found.
java.lang.ClassNotFoundException: MyApplet
at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unknown Source)
at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at java.io.BufferedInputStream.fill(Unknown Source)
at java.io.BufferedInputStream.read1(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at sun.net.www.http.HttpClient.parseHTTPHeader(Unknown Source)
at sun.net.www.http.HttpClient.parseHTTP(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.HttpURLConnection.getResponseCode(Unknown Source)
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)
... 9 more
Exception: java.lang.ClassNotFoundException: MyApplet

小程序代码:

import javax.swing.*;
import java.awt.*;

public class MyApplet extends JApplet {

public JFrame frame;
public JPanel panel;
public JButton button;

public void init() {
    frame = new JFrame();
    panel = new JPanel();
    button = new JButton("click me ");
    panel.setBackground(Color.RED);
    panel.add(button);
    frame.add(panel);
    frame.setSize(300,300);
    frame.setVisible(true);
}   
}

html代码:

<applet code="MyApplet" codebase="AppletPackage" archive="JAR.jar" height="800" width="800">

JAR.jar 文件包含一个包 Appletpackage,此包包含一个名为的类文件MyApplet.class

在此处输入图像描述

为什么我会得到这个异常?我犯了什么错误?

4

2 回答 2

2

参数是相对于archive参数解析的codebase。因此,在您的情况下,插件将查找MyApplet.class包含在 file 中的文件AppletPackage/JAR.jar

您应该将其更改为以下内容:

<applet code="AppletPackage.MyApplet" archive="JAR.jar" height="800" width="800">

这将解析到AppletPackage/MyApplet.classJAR.jarHTML 文件相同的目录中。

于 2012-07-09T14:49:39.457 回答
1

这是试图解决对我的第一个答案的评论中报告的错误消息:

java.lang.NoClassDefFoundError: AppletPackage/MyApplet (wrong name: MyApplet)

查看源代码,我看到这个“错误名称”错误消息表明文件名和类名不匹配。您声称您的课程在 inside AppletPackage,并且文件名AppletPackage/MyApplet.class符合此要求。但是您上面引用的源代码没有包含一行

package AppletPackage;

您应该添加该行,以便类文件包含类的完全限定名。然后你应该能够加载它。

于 2012-07-09T17:02:30.893 回答