Java applet Error ... What is wrong?

10,533

Solution 1

Problem is with the package. You need to change the code attribute of applet, and based on where you have placed your HTML, the codebase attribute too. You have to place HellowApplet.class in a directory called M257Applet (because that is the package you have given), and the applet tag should look something like:

<applet code="M257Applet.HellowApplet" ... ></applet>

For this to work, your HTML has to be in the same directory as M257Applet (not inside M257Applet). Alternatively, you can specify the codebase attribute. For eg, with the following directory structure:

somedir
  +-- hello.html
  +-- M257Applet
  |    +-- HellowApplet.class

the applet will work. If however, you had

anotherdir
  +-- hello.html
  +-- somedir
  |   +-- M257Applet
  |   |    +-- HellowApplet.class

then you will have to specify codebase attribute like so:

<applet code="M257Applet.HellowApplet" codebase="somedir" ... ></applet>

So, you should have codebase pointing to the directory containing your package, and code has to have your package name also in it.

Edit: Please note, even though code="HellowApplet.class" will work, the correct way of specifying the applet is without the ".class" at the end.

Solution 2

Your class is in a package. It's file name should match.

code="M257Applet/HellowApplet.class"

(It's a good idea to follow conventions. Package names should be all lower case.)

Share:
10,533
Prince
Author by

Prince

Updated on June 25, 2022

Comments

  • Prince
    Prince almost 2 years

    Java applet code

    package M257Applet
    
    import java.applet.*;
    
    import javax.swing.*;
    
    import java.awt.*;
    
    public class HellowApplet extends JApplet {
    
        public  void init(){
    
            Container cp = getContentPane();
            JLabel lb = new JLabel("Hellowwwww");
            cp.add(lb);
        }
    
    }
    

    html file

    <html>
    <head>
    <title>Applet</title>
    </head>
    <body>
    <APPLET CODE = HellowApplet.class  WIDTH =  400   HEIGHT = 400 >
    </APPLET>
    </body>
    </html>
    

    Error

    Java Plug-in 1.6.0_22
    Using JRE version 1.6.0_22-b04 Java HotSpot(TM) Client VM
    User home directory = C:\Users\pc
    ----------------------------------------------------
    c:   clear console window
    f:   finalize objects on finalization queue
    g:   garbage collect
    h:   display this help message
    l:   dump classloader list
    m:   print memory usage
    o:   trigger logging
    q:   hide console
    r:   reload policy configuration
    s:   dump system and deployment properties
    t:   dump thread list
    v:   dump thread stack
    x:   clear classloader cache
    0-5: set trace level to <n>
    ----------------------------------------------------
    
    
    java.lang.NoClassDefFoundError: HellowApplet (wrong name: M257Applet/HellowApplet)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClassCond(Unknown Source)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
        at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Unknown Source)
        at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
        at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unknown Source)
        at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
        at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)
    Exception: java.lang.NoClassDefFoundError: HellowApplet (wrong name: M257Applet/HellowApplet)
    
    • Prince
      Prince about 13 years
      <html> <head> </head> <body> <APPLET CODE = "HellowApplet.class" WIDTH = 400 HEIGHT = 400 > </APPLET> </body> </html>
    • Andrew Thompson
      Andrew Thompson about 13 years
      JLabel("Hellowwwww"); Note the word 'hello' does not have a 'w', let alone 4 of them!
    • trashgod
      trashgod about 13 years
      @Andrew Thompson: "Hellowwwww" is an onomatopoetic echo and possibly the name of a band. :-)
  • Andrew Thompson
    Andrew Thompson about 13 years
    Good description of the paths. Note that the code attribute of M257Applet.HellowApplet.class should be the fully qualified name of the class, so M257Applet.HellowApplet. And of course taking into account common nomenclature & spelling - m257applet.HelloApplet ;)
  • Andrew Thompson
    Andrew Thompson over 11 years
    code="M257Applet.HellowApplet.class" Should ideally be code="M257Applet.HellowApplet". It represents the fully qualified class name. The 1st form is tolerated, but not correct.
  • Ryan S
    Ryan S about 11 years
    @Raze - What if my class folders is 6 folders within the root, where would the extra folders go, within the codebase?
  • ardeee
    ardeee about 11 years
    I didn't understand what exact structure you have @RyanSammut, but assuming you have defined the applet in package foo.bar, and you have it in the folder root/f1/f2/f3/f4/foo/bar/Applet.class and HTML in root/, your codebase="f1/f2/f3/f4" and code="foo.bar.Applet" also assuming your HTML file is directly in root.