简体   繁体   English

NoClassDefFound错误,java

[英]NoClassDefFound error, java

My class's code: 我班级的代码:

package overviewPack;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ButtonScreen extends JApplet implements ActionListener{

    JButton middle = new JButton();
    Container screen = getContentPane();
    public void init(){

        setVisible(true);
        middle.addActionListener(this);
        screen.add(middle);
    }

    public void actionPerformed(ActionEvent event) {
        if (event.getSource() == middle){
            System.out.println("hey");
        }

    }
}

When I attempt to run it using html, I recieve a noclassdefFound error, with the stacktrace as ButtonScreen(Wrong name: overviewPack ButtonScreen) 当我尝试使用html运行它时,我收到一个noclassdefFound错误,堆栈跟踪为ButtonScreen(错误的名称:overviewPack ButtonScreen)

Here is my html code: (I use brackets so that the code will turn up in chat as the code and not the finished product). 这是我的HTML代码:(我使用括号,以便代码将作为代码而不是成品出现在聊天中)。

<HEAD>
<TITLE>
A Simple Program </TITLE>
</HEAD>
<BODY>
Here is the output of my program:
<APPLET CODE="overviewPack.ButtonScreen.class" codebase = "bin" WIDTH=150 HEIGHT=25>
</APPLET>
</BODY>
</HTML>

I have tried many different formats for the html, and looked at many other people's similar, sometimes exactly the same errors, but none of the solutions proposed to other people have worked. 我已经为html尝试了许多不同的格式,并查看了许多其他人类似的,有时完全相同的错误,但没有提出给其他人的解决方案。 I have also looked around on the rest of the net for a solution, but I have found none. 我也在网络的其余部分四处寻找解决方案,但我没有找到。

This error happens with all my applets, even this extremely simple applet I did above. 这个错误发生在我的所有applet上,即使是我在上面做的这个非常简单的applet。

The html file is in the same folder as the class html文件与类位于同一文件夹中

The HTML file is in the same folder as the class HTML文件与类位于同一文件夹中

That is no good. 那不好。 You need to understand how the parameters in the applet element work. 您需要了解applet元素中的参数如何工作。

<APPLET CODE="overviewPack.ButtonScreen.class" codebase="bin" WIDTH=150 HEIGHT=25>

Let us say that the HTML is located at: our.com/applets/applet1.html . 我们假设HTML位于: our.com/applets/applet1.html

codebase = "bin" would mean the classpath starts with our.com/applets/bin/ . codebase = "bin"表示类路径以our.com/applets/bin/

overviewPack.ButtonScreen.class would therefore need to be found at: 因此,需要在以下位置找到overviewPack.ButtonScreen.class

our.com/applets/bin/overviewPack/ButtonScreen.class

Note that the package overviewPack has become an inherent part of the correct path to the class file. 请注意,包overviewPack已成为类文件的正确路径的固有部分。 That is where the 'wrong name' is originating from. 就是“错误名称”的起源。 The JRE seems to be searching the directory of the HTML, locating the class in the same directory, then loading it to discover it is in the wrong path. JRE似乎正在搜索HTML的目录,将类定位在同一目录中,然后加载它以发现它在错误的路径中。

Code Attribute 代码属性

<APPLET CODE="overviewPack.ButtonScreen.class" codebase="bin" WIDTH=150 HEIGHT=25>

Note the required value is the Fully Qualified Name of the class file. 请注意,所需的值是类文件的完全限定名称。 That consists of the package(s) name, followed by the class name, each separated by a dot. 它由包名称后跟类名称组成,每个名称用点分隔。 EG 例如

overviewPack.ButtonScreen 

As opposed to 相反

overviewPack.ButtonScreen.class // combination of FQN with file type

or 要么

overviewPack/ButtonScreen.class // relative file path on server

So the opening APPLET element should best be: 所以开放的APPLET元素最好是:

<APPLET CODE="overviewPack.ButtonScreen" codebase="bin" WIDTH=150 HEIGHT=25>

Sometimes there is a problem with the .class file extension at the end of the code= attribute. 有时在code =属性末尾的.class文件扩展名存在问题。 Some doc I've seen says that the code= attribute has the classname in which case having the .class at the end is wrong. 我见过的一些文档说code = attribute有classname,在这种情况下,末尾的.class是错误的。 The classname is: overviewPack.ButtonScreen and the filename is: ButtonScreen.class 类名是:overviewPack.ButtonScreen,文件名是:ButtonScreen.class

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM