Java小程序代码
package M257Applet
import java.applet.*;
import javax.swing.*;
import java.awt.*;
public class HellowApplet extends JApplet {
public void init(){
Container cp = getContentPane();
JLabel lb = new JLabel("Hellowwwww");
cp.add(lb);
}
}
html文件
<html>
<head>
<title>Applet</title>
</head>
<body>
<APPLET CODE = HellowApplet.class WIDTH = 400 HEIGHT = 400 >
</APPLET>
</body>
</html>
错误
Java Plug-in 1.6.0_22
Using JRE version 1.6.0_22-b04 Java HotSpot(TM) Client VM
User home directory = C:\Users\pc
----------------------------------------------------
c: clear console window
f: finalize objects on finalization queue
g: garbage collect
h: display this help message
l: dump classloader list
m: print memory usage
o: trigger logging
q: hide console
r: reload policy configuration
s: dump system and deployment properties
t: dump thread list
v: dump thread stack
x: clear classloader cache
0-5: set trace level to <n>
----------------------------------------------------
java.lang.NoClassDefFoundError: HellowApplet (wrong name: M257Applet/HellowApplet)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
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)
Exception: java.lang.NoClassDefFoundError: HellowApplet (wrong name: M257Applet/HellowApplet)
答案 0 :(得分:12)
问题在于包裹。您需要更改applet的代码属性,并根据放置HTML的位置,更改代码库属性。您必须将HellowApplet.class放在名为M257Applet的目录中(因为这是您提供的包),并且applet标记应如下所示:
<applet code="M257Applet.HellowApplet" ... ></applet>
为此,您的HTML必须与M257Applet位于同一目录中(不在M257Applet内)。或者,您可以指定codebase属性。例如,使用以下目录结构:
somedir
+-- hello.html
+-- M257Applet
| +-- HellowApplet.class
小程序将起作用。但是,如果你有
anotherdir
+-- hello.html
+-- somedir
| +-- M257Applet
| | +-- HellowApplet.class
然后你必须像这样指定codebase属性:
<applet code="M257Applet.HellowApplet" codebase="somedir" ... ></applet>
因此,您应该codebase
指向包含您的包的目录,并且code
也必须包含您的包名称。
编辑:
请注意,即使code="HellowApplet.class"
可行,指定applet的正确方法也不是最后的“.class”。
答案 1 :(得分:2)
你的班级在一个包中。它的文件名应该匹配。
code="M257Applet/HellowApplet.class"
(遵循惯例是一个好主意。包名称应该都是小写的。)