java Java小程序错误...有什么问题?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6009767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 13:57:17  来源:igfitidea点击:

Java applet Error ... What is wrong?

javahtmlswingjapplet

提问by Prince

Java applet code

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 file

html文件

<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)

回答by Raze

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:

问题出在包上。您需要更改小程序的 code 属性,并根据您放置 HTML 的位置,更改 codebase 属性。您必须将 HellowApplet.class 放在名为 M257Applet 的目录中(因为这是您提供的包),并且小程序标记应如下所示:

<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:

为此,您的 HTML 必须与 M257Applet 位于同一目录中(不在 M257Applet 内)。或者,您可以指定代码库属性。例如,具有以下目录结构:

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 codebasepointing to the directory containing your package, and codehas to have your package name also in it.

因此,您应该codebase指向包含您的包的目录,并且code必须在其中包含您的包名称。

Edit: Please note, even though code="HellowApplet.class"will work, the correct way of specifying the applet is without the ".class" at the end.

编辑:请注意,即使code="HellowApplet.class"可以工作,指定小程序的正确方法是末尾没有“.class”。

回答by Tom Hawtin - tackline

Your class is in a package. It's file name should match.

您的课程在一个包中。它的文件名应该匹配。

code="M257Applet/HellowApplet.class"

(It's a good idea to follow conventions. Package names should be all lower case.)

(遵循约定是个好主意。包名称应全部小写。)