Madara's Ghost
Madara's Ghost

Reputation: 175088

Java applet error

I am just starting to learn Java (woohoo!!) and I'm reading a book called Introduction to Programming Using Java.

The book has some examples, for instance, on how to create applets, I've been trying to do it myself to no success, even after try to copy their example, it still doesn't work. I write the class and compile it, then I copy the class to the same folder as my html file and state

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

The code is as follows:

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

(This is the copied version)

Now, I get the following error:

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

Note that AnimationBase is a class defined elsewhere in the project which extends JApplet, and its class is included in the same directory.

What am I doing wrong? (it's probably some nooby error, but then again, I am a noob in Java).

Will appreciate any and all answers, thanks in advance :)

EDIT: Oh yeah, I'm using JDK 1.7.0 with NetBeans 7.0.1.

Upvotes: 0

Views: 2186

Answers (3)

Ben
Ben

Reputation: 2388

The package name must be present in three places:

  • in the Java source file

    package helloworld;

  • in the applet tag:

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

  • as a subdirectory of your codebase directory, which by default is the directory containing your HTML file:

    blah/mypage.html

    blah/helloworld/StaticRects.class

If you can get a different applet to work in a named package, then the problem must be with the code. Your StaticRects class works fine for me if I use the following minimal AnimationBase class:

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

My directory hierarchy looks like this:

html/: applet.html helloworld

html/helloworld: AnimationBase.class StaticRects.class

Upvotes: 1

Michael Lorton
Michael Lorton

Reputation: 44436

Yup, noob error: you are creating an class called helloworld.StaticRects (notice the package helloworld statement) and then referring to just StaticRects. Taking out the package statement will fix it.

EDIT: James mentions the exact opposite way to fix the problem, by referring to helloworld.StaticRects.class. Your call, of course, but I'm voting for my way because you don't have any evident need for a package (some people would say you always need some package).

Upvotes: 1

James Black
James Black

Reputation: 41848

For the basic error you can look at this for more on the applet tag:

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

This is the important part:

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.

try using helloworld.StaticRects.class

Here is another example where they use a package name:

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

Upvotes: 1

Related Questions