小程序问题 - NoClassDefFoundError [英] Applet trouble - NoClassDefFoundError

查看:20
本文介绍了小程序问题 - NoClassDefFoundError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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

我的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);
      }
   }
}

两个文件在同一个目录

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

如果我通过 Eclipse 自行运行小程序,它运行良好.当我打开页面时出现错误

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

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>

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

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. Other answers point 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?

推荐答案

  • /home/me/workspace/myProject/bin
    • applet.html
    • /home/me/workspace/myProject/bin/test

      1. SimpleApplet.class

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

      If the class SimpleApplet is in package test put the HTML in the parent directory (as detailed above), and use this 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>
      

      小贴士:

      • 发布代码时,同时发布导入和包语句,或者换句话说,全部.事实上,我们需要对事物进行猜测;但是有了完整的代码,我们就不必这样做了.
      • 不要在这个阶段尝试小程序.您应该解决在命令行上使用包的问题,​​Applet 比使用 GUI 的应用程序更难.
      • 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.
        • 这篇关于小程序问题 - NoClassDefFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆