Applet trouble - NoClassDefFoundError
Asked Answered
B

3

5

I realize there's a million of these posts but none of them have helped me so here goes: I'm trying to deploy a very, very simple applet that will not load properly. My HTML:

<html>
<head>
    <meta http-equiv="Content-Type" content"text/html; charset=utf-8">
</head>
<body>
   <applet code = "SimpleApplet.class"
   width = "320" height = "100"></applet>
</body>
</html>

My Java:

package test;

import javax.swing.*;   

public class SimpleApplet extends JApplet{
   public void init(){
      try{
        SwingUtilities.invokeAndWait(new Runnable(){
          public void run(){
            JLabel lbl = new JLabel("Hello World");
            add(lbl);
          }
        });             
      }
      catch(Exception e){
        System.out.println(e);
      }
   }
}

Both files are located in the same directory

/home/me/workspace/myProject/bin/test

If I run the applet on its own via Eclipse, it works fine. When I open the page I get the error

java.lang.NoClassDefFoundError: SimpleApplet (wrong name: test/SimpleApplet)

The error would suggest that I have incorrectly placed or named something. However, after trying

<applet code = "test/SimpleApplet.class"
width = "320" height = "100"></applet>

<applet code = "SimpleApplet.class"
codebase = "/test"
width = "320" height = "100"></applet>

along with other attempts, including removing the ", trying absolute and all partial path names, and using .java, it still does not work and I end up getting a ClassNotFoundException. Other answers point out that classpath and codebase (often relating to archive) issues are a primary reason for this occurring. However, I am not using a jar file and both files are in the same directory. Anyone know why this is occurring?

Briony answered 27/7, 2012 at 17:3 Comment(1)
It seems to be saying that you have a .class file named "SimpleApplet" that internally says it's name is "test/SimpleApplet". Ie, you've declared it to be in the "test" package, but you have not positioned it in the "test" directory inside the jar or class directory. For instance, you may have specified "mystuff/mydir/test" in your classpath when you should have said just "mystuff/mydir".Ketch
V
7
  • /home/me/workspace/myProject/bin
    • applet.html
    • /home/me/workspace/myProject/bin/test
      1. SimpleApplet.class

If the class SimpleApplet is in package test put the HTML in the parent directory (as detailed above), and use this HTML.

<html>
<head>
    <meta http-equiv="Content-Type" content"text/html; charset=utf-8">
</head>
<body>
   <applet code = "test.SimpleApplet"
   width = "320" height = "100"></applet>
</body>
</html>

Side tips:

  • When posting code, post the imports and package statement as well, or in other words, all of it. As it is we need to make guesses about things; but with the full code, we would not have to.
  • Don't attempt applets at this stage. You should sort out working with packages on the command line, and applets are more difficult than applications with a GUI.
Violative answered 29/7, 2012 at 0:30 Comment(0)
G
0

Judging from

along with other attempts, including removing the ", trying absolute and all partial path names, and using .java

You are attempting to source test/SimpleApplet.java rather than test/SimpleApplet.class. You will need to compile your SimpleApplet.java file into a SimpleApplet.class file

Make sure that when you do, you use

<applet code = "SimpleApplet.class"
codebase = "/test"
width = "320" height = "100"></applet>

As /test is the codebase, rather than the package name.

Graiae answered 27/7, 2012 at 17:48 Comment(0)
S
0

NoClassDefFoundError: Ahmed (wrong name: intoapplet/Ahmed )

I've been searching for a solution for 3 days .

Your advice:

If you have named your class 'test' Coding convention recommends it should be 'Test'.

This helped me along with not including the package declaration in my Java class. By saving the project with the first letter of it's name as capital.(using eclipse).

----------------------------------Java code: --------------------------------

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

    public  class  Ahmed extends JApplet {

    private static final long serialVersionUID = 1L;

    public void paint(Graphics g){
    super.paint(g);
    g.drawString("my name is Ahmed Zaki wow this actually
    works",300,300);                     
    }
    }

----------------------------------HTML code----------------------------------

    <html>
    <head>
    <title>Tutorial applet</title>
    </head>
    <body>

    <Applet code="Ahmed.class"   width="400"; height="300";> 

    </Applet>
    </body>
    </html>

-------Directory on the open-source SFTP , FTP, WebDAV and SCP client-------

Both , class & html file , in the /public_html/ folder by using "WinSCP" which is a free and open-source SFTP, FTP, WebDAV and SCP client for Microsoft Windows. Its main function is secure file transfer between a local and a remote computer.

    example:

    directory for html file = /public_html/index.html
    directory for class file = /public_html/Ahmed.class

    example no2:

    +-- public_html
    |   +-- index.html
    |   +-- Ahmed.class
Sorgo answered 3/7, 2015 at 21:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.