Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
240 views
in Technique[技术] by (71.8m points)

java - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  • /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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...