Java applet错误...什么是错误的? [英] Java applet Error ... What is wrong?
问题描述
Java applet代码
package M257Applet
import java。 。小程序。*;
import javax.swing。*;
import java.awt。*;
$ b public class HellowApplet扩展JApplet {
public void init(){
Container cp = getContentPane();
JLabel lb = new JLabel(Hellowwwww);
cp.add(lb);
}
}
html文件 strong>
< 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
使用JRE版本1.6.0_22-b04 Java HotSpot™客户端VM
用户主目录= C:\ Users \pc
------------------------------------------- ---------
c:清除控制台窗口
f:在终结队列上敲定对象
g:垃圾收集
h:显示帮助信息
l:dump classloader列表
m:打印内存使用情况
o:触发日志记录
q:隐藏控制台
r:重新加载策略配置
s:转储系统和部署属性$ b $:转储线程列表
v:转储线程堆栈
x:清除类加载程序缓存
0-5:将跟踪级别设置为< n>
---------------------------------------------- ------
java.lang.ClassLoader.defineClass1(Native Method)
java.lang.NoClassDefFoundError:HellowApplet(错误名称:M257Applet / HellowApplet)$ b java.lang.ClassLoader.defineClassCond(未知源)$ b $ java.lang.ClassLoader.defineClass(未知源)$ b $ java.security.SecureClassLoader.defineClass(未知源)
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(未知源)
在sun.plugin2.applet.Plugin2Manager.createApplet(未知源)
在sun.plugin2.applet.Plugin2Manager $ AppletExecutionRunn (未知源)
在java.lang.Thread.run(未知源)
异常:java.lang.NoClassDefFoundError:HellowApplet(错误名称:M257Applet / HellowApplet)
问题在于包。您需要更改applet的代码属性,并根据您放置HTML的位置,代码属性。您必须将HellowApplet.class放在名为M257Applet的目录中(因为这是您提供的软件包),并且applet标签应该如下所示:
< applet code =M257Applet.HellowApplet...>< / applet>
为此,您的HTML必须位于与M257Applet相同的目录中(不在M257Applet中) 。或者,您可以指定代码库属性。例如,使用以下目录结构:
somedir
+ - hello.html
+ - - M257Applet
| + - HellowApplet.class
小程序可以工作。但是,如果您有
$ pre code $ anotherdir
+ - hello.html
+ - somedir
| + - M257Applet
| | + - HellowApplet.class
然后您必须指定codebase属性,如下所示:
< applet code =M257Applet.HellowAppletcodebase =somedir...>< / applet>
所以,你应该有 codebase
指向包含你的包的目录和 code
必须包含你的包名称。
编辑:
请注意,即使 code =HellowApplet.class
指定applet的正确方法是在末尾没有.class。
Java applet code
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 file
<html>
<head>
<title>Applet</title>
</head>
<body>
<APPLET CODE = HellowApplet.class WIDTH = 400 HEIGHT = 400 >
</APPLET>
</body>
</html>
Error
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)
Problem is with the package. You need to change the code attribute of applet, and based on where you have placed your HTML, the codebase attribute too. You have to place HellowApplet.class in a directory called M257Applet (because that is the package you have given), and the applet tag should look something like:
<applet code="M257Applet.HellowApplet" ... ></applet>
For this to work, your HTML has to be in the same directory as M257Applet (not inside M257Applet). Alternatively, you can specify the codebase attribute. For eg, with the following directory structure:
somedir
+-- hello.html
+-- M257Applet
| +-- HellowApplet.class
the applet will work. If however, you had
anotherdir
+-- hello.html
+-- somedir
| +-- M257Applet
| | +-- HellowApplet.class
then you will have to specify codebase attribute like so:
<applet code="M257Applet.HellowApplet" codebase="somedir" ... ></applet>
So, you should have codebase
pointing to the directory containing your package, and code
has to have your package name also in it.
Edit:
Please note, even though code="HellowApplet.class"
will work, the correct way of specifying the applet is without the ".class" at the end.
这篇关于Java applet错误...什么是错误的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!