Appletviewer引发java.lang.NoClassDefFoundError异常
我想在Appletviewer引发java.lang.NoClassDefFoundError异常,java,applet,noclassdeffounderror,Java,Applet,Noclassdeffounderror,我想在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; impor
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>
applet标记的code参数应该是Java类的全名,因此
com.examples.ti.applet
。请参见Now I get:java.lang.ClassNotFoundException:com.examples.ti.applet您有引用的jar文件吗?html文件也应该在根目录下,在bin文件夹中。我不得不把html文件放在bin的根目录下,谢谢你的帮助!两分钟后接受答案。为什么我不能把HTML放在同一个文件夹中?你确定这个类在jar中,有正确的文件夹结构吗?否则,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)