Java 小程序错误

Java applet error

提问人:Madara's Ghost 提问时间:8/29/2011 最后编辑:Madara's Ghost 更新时间:6/13/2016 访问量:2103

问:

我刚刚开始学习Java(呜呜!!),我正在读一本名为《Introduction to Programming Using Java》的书。

书中有一些例子,比如说,关于如何创建小程序,我一直在尝试自己做,但没有成功,即使尝试复制他们的例子,它仍然不起作用。我编写类并编译它,然后将类复制到与我的 html 文件和状态相同的文件夹中

<applet code="StaticRects.class" height=160 width=300></applet>

代码如下:

package helloworld;

import java.awt.*;

/**
 *
 * @author RikudoSennin
 */
public class StaticRects extends AnimationBase {

    public void drawFrame(Graphics g) {
// Draw set of nested black rectangles on a red background.
// Each nested rectangle is separated by 15 pixels on all sides
// from the rectangle that encloses it. The applet is
// assumed to be 300 pixels wide and 160 pixels high.
        int inset; // Gap between borders of applet and one of the rectangles.
        int rectWidth, rectHeight; // The size of one of the rectangles.
        g.setColor(Color.red);
        g.fillRect(0, 0, 300, 160); // Fill the entire applet with red.
        g.setColor(Color.black); // Draw the rectangles in black.
        inset = 0;
        rectWidth = 299; // Set size of the first rect to size of applet
        rectHeight = 159;
        while (rectWidth >= 0 && rectHeight >= 0) {
            g.drawRect(inset, inset, rectWidth, rectHeight);
            inset += 15; // rects are 15 pixels apart
            rectWidth -= 30; // width decreases by 15 pixels on left and 15 on right
            rectHeight -= 30; // height decreases by 15 pixels on top and 15 on bottom
        }
    } // end paint()
} // end class StaticRects

(这是复制版本)

现在,我收到以下错误:

java.lang.NoClassDefFoundError: StaticRects (wrong name: helloworld/StaticRects)

请注意,这是在项目中其他位置定义的类,它扩展了 ,并且其类包含在同一个目录中。AnimationBaseJApplet

我做错了什么?(这可能是一些菜鸟错误,但话又说回来,我是 Java 中的菜鸟)。

将不胜感激任何和所有答案,提前致谢:)

编辑:哦,是的,我正在将JDK 1.7.0与NetBeans 7.0.1一起使用。

Java 小程序

评论

0赞 TeaCupApp 8/29/2011
您使用的是哪个 IDE?
0赞 Madara's Ghost 8/29/2011
看来你错过了我的编辑:)NetBeans 7.0.1 @krio
0赞 TeaCupApp 8/29/2011
是的并发问题哈哈,似乎编译器无法找到您的类文件。答案已经给出,你可以尝试一下。

答:

1赞 James Black 8/29/2011 #1

对于基本错误,您可以查看有关小程序标签的更多信息:

http://download.oracle.com/javase/1.4.2/docs/guide/misc/applet.html

这是重要的部分:

CODE = appletFile
This REQUIRED attribute gives the name of the file that contains the applet's compiled Applet subclass. This file is relative to the base URL of the applet. It cannot be absolute. One of CODE or OBJECT must be present. The value appletFile can be of the form classname.class or of the form packagename.classname.class.

尝试使用helloworld.StaticRects.class

下面是他们使用包名称的另一个示例:

http://download.oracle.com/javase/tutorial/deployment/applet/html.html

评论

0赞 Madara's Ghost 8/29/2011
给出不同的错误:Exception: java.lang.ClassNotFoundException: helloworld.StaticRects.class
1赞 Michael Lorton 8/29/2011 #2

是的,菜鸟错误:您正在创建一个名为 helloworld 的类。StaticRects(注意语句),然后仅引用 StaticRects。取出 package 语句将修复它。package helloworld

编辑:James提到了完全相反的解决问题的方法,即提到helloworld。StaticRects.class。当然,你的电话,但我投票支持我的方式,因为你对包没有任何明显的需求(有些人会说你总是需要一些包)。

评论

0赞 Madara's Ghost 8/29/2011
删除包名称后,它仍然有相同的错误(奇怪的是,包括helloworld),即使我“清理并重建”了它。java.lang.NoClassDefFoundError: StaticRects (wrong name: helloworld/StaticRects)
0赞 Michael Lorton 8/29/2011
该错误告诉您您尚未清理和重建它 - 或者至少,您的浏览器拥有的类文件是旧的。请注意,字符串“helloworld”仅存在于假定已删除的代码和错误消息中。清除缓存。
0赞 Madara's Ghost 8/29/2011
手动删除所有类文件,重建它们,将它们复制到我的测试页面的目录中,从我的浏览器中清除所有缓存后,我仍然收到相同的错误:)
0赞 Madara's Ghost 8/29/2011
另外,既然你提到了它,*.php 文件就不会被缓存。
0赞 Michael Lorton 8/29/2011
字符串位于某个地方,否则 JVM 会做出一个非常幸运的猜测。grep 测试页面目录中的类文件;使用 Firebug 检查 GET 的结果;用于手动下载类文件。也许在浏览器和服务器之间的某个地方有一个缓存服务器。helloworldcurl
1赞 Ben 8/31/2011 #3

包名称必须存在于三个位置:

  • 在 Java 源文件中

    package helloworld;

  • 在 applet 标记中:

    <applet code="helloworld.StaticRects" height=160 width=300></applet>

  • 作为代码库目录的子目录,默认情况下,该目录是包含 HTML 文件的目录:

    blah/mypage.html

    blah/helloworld/StaticRects.class

如果可以让不同的小程序在命名包中工作,那么问题一定出在代码上。如果我使用以下最小类,您的类对我来说效果很好:StaticRectsAnimationBase

package helloworld;

import java.awt.Graphics;
import javax.swing.JApplet;

public abstract class AnimationBase extends JApplet {
    public void paint(Graphics g) {
        this.drawFrame(g);
    }

    public abstract void drawFrame(Graphics g);
}

我的目录层次结构如下所示:

html/: 小程序.html helloworld

html/helloworld: 动画库.class StaticRects.class

评论

0赞 Madara's Ghost 8/31/2011
但是,即使我从源代码中删除了包,清理并重建了它,它仍然有问题,这是怎么回事?不过,我会尝试你的解决方案。
0赞 Madara's Ghost 8/31/2011
当我尝试您的解决方案时,它会给出“小程序无法加载”错误,但控制台中没有任何显示......
0赞 Ben 8/31/2011
你是如何测试小程序的?如果您使用的是浏览器,则每次进行更改时都可能需要重新启动它。上次我写一个小程序时,我找不到任何其他方法让Firefox可靠地加载新代码。
0赞 Madara's Ghost 8/31/2011
我在 chrome(没有关闭它)和 firefox(专门为它启动它)中都尝试过它
0赞 Madara's Ghost 8/31/2011
嗯,我在控制台中收到此错误:Detected from bootclasspath: C:\\PROGRA~2\\Java\\jre7\\lib\\deploy.jar