Java applet 错误...出了什么问题?

发布于 2024-11-07 05:02:41 字数 2294 浏览 2 评论 0原文

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)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜还是长夜 2024-11-14 05:02:41

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

<applet code="M257Applet.HellowApplet" ... ></applet>

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

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" 可以工作,指定小程序的正确方法是末尾不带“.class”。

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.

音盲 2024-11-14 05:02:41

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

code="M257Applet/HellowApplet.class"

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文