gpt4 book ai didi

java - Appletviewer 抛出 java.lang.NoClassDefFoundError 异常

转载 作者:行者123 更新时间:2023-11-30 03:21:17 25 4
gpt4 key购买 nike

我想在 appletviewer 中运行 applet。我的项目目录具有以下结构:

/home/sanctus/workspace/AppletDocumentLoader/bin/com/examples/ti

AppletDocumentLoader 是我的项目,然后在 /bin/com/examples/ti 中有 3 个类文件和 HTML 文件,即:全部位于同一目录中。

我的 src 类

package com.examples.ti;
import java.applet.AppletContext;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

public class Applet extends JApplet {

private static final long serialVersionUID = -8756947240188460854L;


public void init() {
try {
SwingUtilities.invokeAndWait(new Runnable() {

@Override
public void run() {
JLabel label = new JLabel("Hello World");
add(label);
JButton openDocument = new JButton();
openDocument.setText("Button");
openDocument.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
System.out.println("BUTTON CLICKED");
AppletContext appletContext = getAppletContext();
try {
appletContext
.showDocument(
new URL(
"http://www.google.com"),
"_self");
} catch (MalformedURLException e1) {
e1.printStackTrace();
}

}
});
add(openDocument);
}
});
} catch (InvocationTargetException | InterruptedException e) {
e.printStackTrace();
}
}
}

我的 HTML 文件

<!DOCTYPE html>
<html>
<head>
<title>Hi there</title>
</head>
<body>
This is a page
a simple page
</body>
<applet code="Applet.class"
archive="Applet.jar"
width=350 height=350>
</applet>
</html>

我得到的错误:

sanctus@sanctus-desktop:~$ appletviewer '/home/sanctus/workspace/AppletDocumentLoader/bin/com/examples/ti/index.html' 
java.lang.NoClassDefFoundError: Applet (wrong name: com/examples/ti/Applet)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at sun.applet.AppletClassLoader.findClass(AppletClassLoader.java:217)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.applet.AppletClassLoader.loadClass(AppletClassLoader.java:152)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at sun.applet.AppletClassLoader.loadCode(AppletClassLoader.java:626)
at sun.applet.AppletPanel.createApplet(AppletPanel.java:799)
at sun.applet.AppletPanel.runLoader(AppletPanel.java:728)
at sun.applet.AppletPanel.run(AppletPanel.java:378)
at java.lang.Thread.run(Thread.java:745)

最佳答案

applet 标签的 code 参数应该是 Java 类的全名,即 com.examples.ti.Applet。请参阅Applet code tags and class files

关于java - Appletviewer 抛出 java.lang.NoClassDefFoundError 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31273280/

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