简体   繁体   中英

ClassNotFoundException in java applets?

I am having an issue with applets, which I have been stuck on for hours. I have made a simple calculator applet that runs fine in eclipse. I have also made a simple html document (I have altered it more times you can imagine in efforts to make it work) which incorporates my calculator applet. The problem is, that whenever I hit "open with firefox" on my html document, I get the following error:

Now, I understand that this question has come up before on the internet, but I have been searching through so many forums for so many hours, and I still haven't figured it out.

I have tried many things in an attempt to resolve this.

I have already tried enabling heck Use SSL 2.0 Compatible ClientHello format.

I have tried cleaning the Java cache by going to the Java control panel, clicking Settings under Temporary Internet Files and clicking Delete Files. None of these things did anything.

I also tried saving the java class originally in eclipse into a .java in notepad and put that and the applet into the same directory, which again, did nothing.

And now, instead of my previous attempts of moving the .java file, I have moved the .html file into the src folder where all my code is, as shown in this screenshot:

该目录在我的src工作区中

This directory is in my workspace in src

Here is my html code, which incorporates the java code:

<html>
<head>
<title> Hello </title>
</head>
<body>
<APPLET code = "myappletclass.java" width = "400" height = "100"
alt = "Not working"
>
</APPLET>
</body>
</html>

This is my html code after millions of revisions, so chances are that I have already tried out a different form of this(ie without the alt, or with APPLET in lowercase ... ) but I am completely open to all suggestions.

Here is the java code that goes with it:

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

public class myappletclass extends JApplet {
 private double sum;

 public void init() { //started by the browser when the Java program   (myappletclass.class) is loaded and run by the browser. The programmer does not write a call to the init() method.

    double n1 = 0;
    double n2 = 0;
    boolean successone = false; //keeps try catch running
    while (!successone) {
        try {
            String fn = JOptionPane.showInputDialog("Enter first number:");  //Whatever they enter will be stored in string fn
            n1 = Double.parseDouble(fn);
             successone = true; //wont go through while loop because is true  now!

            sum += n1;
        }

        catch (Exception e) {
            JOptionPane.showMessageDialog(null, "ENTER A NUMBER", "Nu uh",  JOptionPane.WARNING_MESSAGE);

        }
    }
    boolean successtwo = false; //keeps try catch running
    while (!successtwo) {
        try {
            String sn = JOptionPane.showInputDialog("Enter second number:");      //Whatever they enter will be stored in string sn
            n2 = Double.parseDouble(sn);
            successtwo = true; //wont go through while loop because is true now!
            sum += n2;
        }

        catch (Exception e) {
            JOptionPane.showMessageDialog(null, "ENTER A NUMBER", "Nu uh",       JOptionPane.WARNING_MESSAGE);

        }
    }

}

public void paint(Graphics g) {
    super.paint(g); //call superclass of paint
    g.drawString("The sum is " + sum, 25, 25);
}
}

At this point of time I am desperate to figure out how this works. Again, I have spent countless hours on figuring out how to do this, so I would really really appreciate your constructive criticism and feedback about how to display my applet in firefox with my html document. I am excited to hear from you, thank you so, so much.

EDIT WITH APPLETVIEWER USING COMMAND PROMPT

I got it to work with appletviewer, but the whole point of me doing this was to see what it would look like in a real browser like firefox, with all my html and all. Instead, I got the same result as I would have got by just running it in eclipse.

在此处输入图片说明

Thanks so much MadProgrammer for helping me figure out the answer, and preventing me from wasting 10 more hours! Here is what I did: I needed to add a codebase = file://linkatbrowserbar to my tag. After adding in the file:// blah/blah/blah I was confronted with a security popup saying my security won't allow this applet. Then, I went over into the java control panel in security, and added file://blah/blah/blah into the list of things I can pass into security. Then I opened it through my browser and that's it! It worked!

Also note that you will need to first compile it in command prompt to turn it into .class from .java. Then you can do all of these steps! :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM