1

My class's code:

package overviewPack;

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

public class ButtonScreen extends JApplet implements ActionListener{

    JButton middle = new JButton();
    Container screen = getContentPane();
    public void init(){

        setVisible(true);
        middle.addActionListener(this);
        screen.add(middle);
    }

    public void actionPerformed(ActionEvent event) {
        if (event.getSource() == middle){
            System.out.println("hey");
        }

    }
}

When I attempt to run it using html, I recieve a noclassdefFound error, with the stacktrace as ButtonScreen(Wrong name: overviewPack ButtonScreen)

Here is my html code: (I use brackets so that the code will turn up in chat as the code and not the finished product).

<HEAD>
<TITLE>
A Simple Program </TITLE>
</HEAD>
<BODY>
Here is the output of my program:
<APPLET CODE="overviewPack.ButtonScreen.class" codebase = "bin" WIDTH=150 HEIGHT=25>
</APPLET>
</BODY>
</HTML>

I have tried many different formats for the html, and looked at many other people's similar, sometimes exactly the same errors, but none of the solutions proposed to other people have worked. I have also looked around on the rest of the net for a solution, but I have found none.

This error happens with all my applets, even this extremely simple applet I did above.

The html file is in the same folder as the class

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Please always post the entire stack trace of errors, and learn to use the four-spaces indent; you can post correct HTML that way. – chrylis -cautiouslyoptimistic- Aug 27 '13 at 16:15
  • 1
    possible duplicate of [Applet trouble - NoClassDefFoundError](http://stackoverflow.com/questions/11692373/applet-trouble-noclassdeffounderror) – chrylis -cautiouslyoptimistic- Aug 27 '13 at 16:17
  • Seen that one it didnt work :-/ This was the entire stack trace, unless you mean the details bit, which just had a default looking menu thing. – user2722299 Aug 27 '13 at 16:21
  • I said that the html and class were in the same folder, I dunno what else to say, I mean they are both under overviewPack, under bin, but what else do you need/want to know? Sorry if I come off a bit brash/rude I always seem to have this happen in text speak :-/ – user2722299 Aug 27 '13 at 16:28
  • *"said that the html and class were in the same folder"* Indeed you did. It was only after I gave the question a little formatting that I noticed that comment of 'same directory'. :P – Andrew Thompson Aug 27 '13 at 16:47

2 Answers2

2

The HTML file is in the same folder as the class

That is no good. You need to understand how the parameters in the applet element work.

<APPLET CODE="overviewPack.ButtonScreen.class" codebase="bin" WIDTH=150 HEIGHT=25>

Let us say that the HTML is located at: our.com/applets/applet1.html.

codebase = "bin" would mean the classpath starts with our.com/applets/bin/.

overviewPack.ButtonScreen.class would therefore need to be found at:

our.com/applets/bin/overviewPack/ButtonScreen.class

Note that the package overviewPack has become an inherent part of the correct path to the class file. That is where the 'wrong name' is originating from. The JRE seems to be searching the directory of the HTML, locating the class in the same directory, then loading it to discover it is in the wrong path.

Code Attribute

<APPLET CODE="overviewPack.ButtonScreen.class" codebase="bin" WIDTH=150 HEIGHT=25>

Note the required value is the Fully Qualified Name of the class file. That consists of the package(s) name, followed by the class name, each separated by a dot. E.G.

overviewPack.ButtonScreen 

As opposed to

overviewPack.ButtonScreen.class // combination of FQN with file type

or

overviewPack/ButtonScreen.class // relative file path on server

So the opening APPLET element should best be:

<APPLET CODE="overviewPack.ButtonScreen" codebase="bin" WIDTH=150 HEIGHT=25>
Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
1

Sometimes there is a problem with the .class file extension at the end of the code= attribute. Some doc I've seen says that the code= attribute has the classname in which case having the .class at the end is wrong. The classname is: overviewPack.ButtonScreen and the filename is: ButtonScreen.class

NormR
  • 336
  • 3
  • 12
  • *"Sometimes there is a problem with the .class file extension at the end of the code= attribute."* True. The required value is the Fully Qualified Name of the class file. That consists of the package(s) name, followed by the class name, each separated by a dot. E.G. `overviewPack.ButtonScreen` (as opposed to `overviewPack.ButtonScreen.class` or `overviewPack/ButtonScreen.class`). – Andrew Thompson Aug 27 '13 at 18:14