0

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.

3
  • Which IDE you are using? Commented Aug 28, 2011 at 16:33
  • Seemingly you missed my edit :) NetBeans 7.0.1 @krio Commented Aug 28, 2011 at 16:34
  • yeah concurrency issues lol, Seems like compiler is not able to locate your class file. Answers are already given you may try them. Commented Aug 28, 2011 at 16:37

3 Answers 3

1
+50

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

Sign up to request clarification or add additional context in comments.

9 Comments

But how is it that even after I delete the package from the source code, clean and rebuilt it, and it STILL does problems? nevertheless I'll try your solution.
When I try your solution, it gives of a "applet failed to load" error, but nothing shows in the console...
How are you testing the applet? If you are using a browser, you may have to restart it each time you make a change. Last time I wrote an applet I could not find any other way to make Firefox reliably load the new code.
I tried it both in chrome (without closing it) and with firefox (started it up specifically for it)
Hmm, I get this error in the console: Detected from bootclasspath: C:\\PROGRA~2\\Java\\jre7\\lib\\deploy.jar
|
1

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

1 Comment

Gives a different error: Exception: java.lang.ClassNotFoundException: helloworld.StaticRects.class
1

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

7 Comments

After removing the package name, it still has the same error (oddly, include the helloworld), even though I "clean and rebuild"ed it. java.lang.NoClassDefFoundError: StaticRects (wrong name: helloworld/StaticRects)
The error is telling you the you haven't cleaned and rebuilt it -- or at least, the class file that your browser has is the old one. Notice that the string "helloworld" exists only in supposedly deleted code and the error message. Clear your cache.
After manually deleting all class files, rebuilding them, copying them into my test page's directory, clearing all the cache from my browser since forever, I still get the very same error :)
Also, now that you mention it, *.php files don't get cached.
The string helloworld is living somewhere, else the JVM is making a fantastically lucky guess. grep the class file in the test page's directory; use Firebug to examine the results of the GET; use curl to download the class file manually. Maybe there's a cache-server somewhere between your browser and your server.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.