java 小程序问题 - NoClassDefFoundError

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

Applet trouble - NoClassDefFoundError

javaappletnoclassdeffounderror

提问by Daniel

I realize there's a million of these posts but none of them have helped me so here goes: I'm trying to deploy a very, very simple applet that will not load properly. My HTML:

我意识到有 100 万个这样的帖子,但没有一个对我有帮助,所以这里是:我正在尝试部署一个非常非常简单的小程序,它无法正确加载。我的 HTML:

<html>
<head>
    <meta http-equiv="Content-Type" content"text/html; charset=utf-8">
</head>
<body>
   <applet code = "SimpleApplet.class"
   width = "320" height = "100"></applet>
</body>
</html>

My Java:

我的爪哇:

package test;

import javax.swing.*;   

public class SimpleApplet extends JApplet{
   public void init(){
      try{
        SwingUtilities.invokeAndWait(new Runnable(){
          public void run(){
            JLabel lbl = new JLabel("Hello World");
            add(lbl);
          }
        });             
      }
      catch(Exception e){
        System.out.println(e);
      }
   }
}

Both files are located in the same directory

两个文件都在同一个目录下

/home/me/workspace/myProject/bin/test

If I run the applet on its own via Eclipse, it works fine. When I open the page I get the error

如果我通过 Eclipse 自己运行小程序,它工作正常。当我打开页面时出现错误

java.lang.NoClassDefFoundError: SimpleApplet (wrong name: test/SimpleApplet)

The error would suggest that I have incorrectly placed or named something. However, after trying

该错误表明我错误地放置或命名了某些东西。然而,在尝试之后

<applet code = "test/SimpleApplet.class"
width = "320" height = "100"></applet>

<applet code = "SimpleApplet.class"
codebase = "/test"
width = "320" height = "100"></applet>

along with other attempts, including removing the ", trying absolute and all partial path names, and using .java, it still does not work and I end up getting a ClassNotFoundException. Otheranswerspoint out that classpath and codebase (often relating to archive) issues are a primary reason for this occurring. However, I am not using a jar file and both files are in the same directory. Anyone know why this is occurring?

连同其他尝试,包括删除“,尝试绝对和所有部分路径名,并使用 .java,它仍然不起作用,我最终得到一个 ClassNotFoundException。其他答案指出类路径和代码库(通常与存档有关)问题是发生这种情况的主要原因。但是,我没有使用 jar 文件,并且两个文件都在同一目录中。有人知道为什么会这样吗?

回答by Andrew Thompson

  • /home/me/workspace/myProject/bin
    • applet.html
    • /home/me/workspace/myProject/bin/test
      1. SimpleApplet.class
  • /home/me/workspace/myProject/bin
    • 小程序.html
    • /home/me/workspace/myProject/bin/test
      1. SimpleApplet.class

If the class SimpleAppletis in package testput the HTML in the parent directory (as detailed above), and use this HTML.

如果类SimpleApplet在包中,test则将 HTML 放在父目录中(如上所述),并使用此 HTML。

<html>
<head>
    <meta http-equiv="Content-Type" content"text/html; charset=utf-8">
</head>
<body>
   <applet code = "test.SimpleApplet"
   width = "320" height = "100"></applet>
</body>
</html>

Side tips:

侧面提示:

  • When posting code, post the imports and package statement as well, or in other words, all of it. As it is we need to make guesses about things; but with the full code, we would not have to.
  • Don't attempt applets at this stage. You should sort out working with packages on the command line, and applets are more difficult than applications with a GUI.
  • 在发布代码时,同时发布导入和包语句,或者换句话说,全部发布。事实上,我们需要对事物进行猜测;但是有了完整的代码,我们就不必了。
  • 在这个阶段不要尝试小程序。您应该解决在命令行上使用包的问题,​​applet 比带有 GUI 的应用程序更难。

回答by OYRM

Judging from

判断从

along with other attempts, including removing the ", trying absolute and all partial path names, and using .java

以及其他尝试,包括删除“,尝试绝对路径名和所有部分路径名,以及使用 .java

You are attempting to source test/SimpleApplet.java rather than test/SimpleApplet.class. You will need to compile your SimpleApplet.java file into a SimpleApplet.class file

您正在尝试获取 test/SimpleApplet.java 而不是 test/SimpleApplet.class。您需要将 SimpleApplet.java 文件编译为 SimpleApplet.class 文件

Make sure that when you do, you use

确保当你这样做时,你使用

<applet code = "SimpleApplet.class"
codebase = "/test"
width = "320" height = "100"></applet>

As /test is the codebase, rather than the package name.

因为 /test 是代码库,而不是包名。

回答by Ahmed Zaki

NoClassDefFoundError: Ahmed (wrong name: intoapplet/Ahmed )

NoClassDefFoundError: Ahmed (wrong name: intoapplet/Ahmed )

I've been searching for a solution for 3 days .

我一直在寻找解决方案 3 天。

Your advice:

你的建议:

If you have named your class 'test' Coding convention recommends it should be 'Test'.

如果您已将类命名为“测试”,编码约定建议它应为“测试”。

This helped me along with not including the package declaration in my Java class. By saving the project with the first letter of it's name as capital.(using eclipse).

这有助于我在 Java 类中不包含包声明。通过将项目名称的第一个字母保存为大写。(使用 eclipse)。

----------------------------------Java code: --------------------------------

----------------------------------Java代码:------------- -------------------

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

    public  class  Ahmed extends JApplet {

    private static final long serialVersionUID = 1L;

    public void paint(Graphics g){
    super.paint(g);
    g.drawString("my name is Ahmed Zaki wow this actually
    works",300,300);                     
    }
    }

----------------------------------HTML code----------------------------------

----------------------------------HTML 代码-------------- --------------------

    <html>
    <head>
    <title>Tutorial applet</title>
    </head>
    <body>

    <Applet code="Ahmed.class"   width="400"; height="300";> 

    </Applet>
    </body>
    </html>

-------Directory on the open-source SFTP , FTP, WebDAV and SCP client-------

-------开源SFTP、FTP、WebDAV和SCP客户端目录-------

Both , class & html file , in the /public_html/ folder by using "WinSCP" which is a free and open-source SFTP, FTP, WebDAV and SCP client for Microsoft Windows. Its main function is secure file transfer between a local and a remote computer.

使用“WinSCP”在 /public_html/ 文件夹中创建类和 html 文件,WinSCP 是适用于 Microsoft Windows 的免费开源 SFTP、FTP、WebDAV 和 SCP 客户端。它的主要功能是本地和远程计算机之间的安全文件传输。

    example:

    directory for html file = /public_html/index.html
    directory for class file = /public_html/Ahmed.class

    example no2:

    +-- public_html
    |   +-- index.html
    |   +-- Ahmed.class