错误:java.lang.NoClassDefFoundError:Chase(错误名称:pong/Chase)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4780371/
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 07:59:33  来源:igfitidea点击:

Error: java.lang.NoClassDefFoundError: Chase (wrong name: pong/Chase)

java

提问by GamDroid

I have created an applet program using Eclipse IDE. Now im creating .html file as below:

我已经使用 Eclipse IDE 创建了一个小程序程序。现在我正在创建 .html 文件,如下所示:

<html>
<APPLET CODE="Chase.class" width=500 height=400>
</APPLET>
</html>

When Im executing this file the error im getting is:

当我执行这个文件时,我得到的错误是:

java.lang.NoClassDefFoundError: Chase (wrong name: pong/Chase)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.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)

In Eclipse IDE is have created new project and packaged the program into "pong" folder.

在 Eclipse IDE 中创建了新项目并将程序打包到“pong”文件夹中。

Can anybody explain why this error is occuring?

任何人都可以解释为什么会发生此错误?

Edit:

编辑:

Adding few Chase.java code lines, for clarification. It is simple applet:

添加几行 Chase.java 代码行以进行说明。这是一个简单的小程序:

package pong;


import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;



public class Chase extends Applet implements Runnable
{ ...  }

回答by Jigar Joshi

There is no class name Chasein your classpath.

Chase您的类路径中没有类名。

wrong namespecifies there might be mistake with class name with package specified.

wrong name指定指定包的类名可能有误。

if your class declares packagefor example

package例如,如果您的班级声明

package a;
public class MyApplet extends Applet{}

then in HTML give a.MyApplet

然后在 HTML 中给出 a.MyApplet

Update

更新

in your case it seems

在你的情况下似乎

<APPLET CODE="pong.Chase.class" width=500 height=400>

<APPLET CODE="pong.Chase.class" width=500 height=400>

also dir structure should be

目录结构也应该是

- - - - -
      |
      |-your html file
      |-pong folder
          |
          |- Chases.class

will do if package name is pong

如果包名是 pong

回答by Stephen C

I think that the problem is basically as @Jigar Joshi has noted, but with a slight wrinkle to it. I think you have a class whose FQN is "pong.Chase", but you have set up the classpath so that the directory containing "Chase.class" is on the classpath. Then you've told the applet loader to look for a class as "Chase.class".

我认为这个问题基本上就像@Jigar Joshi 所指出的那样,但有一点点皱纹。我认为您有一个 FQN 为“pong.Chase”的类,但是您已经设置了类路径,以便包含“Chase.class”的目录位于类路径中。然后你告诉小程序加载器寻找一个类为“Chase.class”。

The classloader has found the bytecode file, but then when it attempted to load it, it has noticed that the classes FQN is "pong.Chase" rather than "Chase" ... as inferred by the name that you gave. Ergo ... a NoClassDefFoundError, with a message that tells you that the class name is incorrect.

类加载器找到了字节码文件,但是当它尝试加载它时,它注意到类 FQN 是“pong.Chase”而不是“Chase”……正如您提供的名称所推断的那样。因此...a NoClassDefFoundError,带有一条消息,告诉您类名不正确

The fix is to make sure that the parent directory of the "pong" directory is on the classpath, and use:

修复方法是确保“pong”目录的父目录在类路径上,并使用:

<APPLET CODE="pong.Chase.class" width=500 height=400></APPLET>

Alternatively - use the codeBaseattribute.

或者 - 使用codeBase属性。

Alternatively 2 - get rid of the packagedeclaration in your Java class.

或者 2 - 去掉packageJava 类中的声明。

Alternatively 3 - use the <object>element. The <applet>element is deprecated.

或者 3 - 使用<object>元素。该<applet>元素已弃用。

Reference: http://www.w3.org/TR/html401/struct/objects.html

参考:http: //www.w3.org/TR/html401/struct/objects.html

回答by dogbane

You forgot the package part in your applet tag:

您忘记了小程序标签中的包部分:

<APPLET CODE="pong.Chase.class" width=500 height=400>
</APPLET>