Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applet trouble - NoClassDefFoundError

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?

like image 338
Daniel Avatar asked Jul 27 '12 17:07

Daniel


People also ask

How do I fix applet error loading?

Re-launch the web browser. Go to the Java applet. When the "Security Warning" window asking "Do you want to run this application?" appears, if there is a "I accept the risk and want to run this app." option, checkmark it ON first then --> Click the "Run" button. The Java applet should load OK now.

Why applet is not used now?

Applets are a technology that are very much of their time, and they have not aged well. The technology proved to be very difficult to evolve, and so applets have not been considered to be a modern development platform for many years now.

Is applet discontinued?

Java applets were deprecated by Java 9 in 2017.

Is Java applet still supported?

No, there isn't. Java Applets are dead and there is no viable way to run them for the vast majority of users on the public Internet. If you are a software developer, you should abandon applets and start learning a modern full-stack framework. There are many to choose from.


1 Answers

  • /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.
like image 197
Andrew Thompson Avatar answered Sep 27 '22 23:09

Andrew Thompson