简体   繁体   中英

JApplet ClassNotFoundException

I am currently trying to turn my JFrame into a JApplet.

It runs fine in eclipse as an applet but when I try to use it on my website it gives me an error.

Here is my Applet: http://tekhaxs.com/applet.java

You can view my java source there ^^ or below.

here is the error: http://tekhaxs.com/?page_id=146

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JApplet;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import javax.imageio.ImageIO;

public class applet extends JApplet
{
    JButton newBut = new JButton("New");
    JButton backBut = new JButton("Back");
    Font font;

    BufferedImage img = null;
    BufferedImage background = null;

    URL url = null;

    String extension;

    int linkNum = 0;
    int total = 0;
    int backNum = 0;
    String appending;

    ArrayList<String> az = new ArrayList<String>();
    ArrayList<String> history = new ArrayList<String>();



public void init()                  //initialize everything.
{
    this.setLayout(null);

    backBut.addActionListener(new buttonListener());
    this.add(backBut);
    backBut.setBounds(300, 5, 80, 35);

    newBut.addActionListener(new buttonListener());
    this.add(newBut);
    newBut.setBounds(400, 5, 80, 35);

    font = new Font("arial",Font.BOLD,20);
    makeArrays();
    changeUrlExtension();

    try {
        background = ImageIO.read(new URL("http://puu.sh/3a7KY/d2ba48949c.png"));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

class buttonListener implements ActionListener  //Button Listener for next.
{

    @Override
    public void actionPerformed(ActionEvent e) 
    {
        if(e.getSource() == backBut){
            backNum++;
            extension = history.get(total - backNum - 1);
            repaint();
        }else if(e.getSource() == newBut){
            backNum = 0;
            changeUrlExtension();
            history.add(extension);
            total++;
            repaint();
        }
    }

}

public void changeUrlExtension(){
    int a1 = (int) Math.round(Math.random() * 51);
    int a2 = (int) Math.round(Math.random() * 51);
    int a3 = (int) Math.round(Math.random() * 51);
    String aaa = (az.get(a3)+az.get(a2)+az.get(a1));
    int linkNum = (int) Math.round(Math.random() * 13) + 20;

    extension = linkNum+aaa;

    try {
        url = new URL("http://puu.sh/"+extension+".png");
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
}

public void paint(Graphics g) {                 //Paints Graphics for frame.
   g.drawImage(background, 0, 0, null);
   g.drawImage(getImage(), 5, 50, null);
   g.setColor(Color.MAGENTA);
   g.drawString("Current Picture: http://puu.sh/"+extension+".png", 10,40);
   g.setFont(font);
   g.drawString("Picture Number: "+(total - backNum), 10,20);
}

public Image getImage(){                        //Returns Image from url.
    try {
        url = new URL("http://puu.sh/"+extension+".png");
    } catch (IOException e) {
        e.printStackTrace();
    }


    try {
        img = ImageIO.read(url);
        System.out.println(total+". "+url);
    } catch (IOException e) {
        changeUrlExtension();
        getImage();
    }

    return img;
}

public void makeArrays(){                       //Makes az Array.
      az.add("A");
      az.add("a");
      az.add("B");
      az.add("b");
      az.add("C");
      az.add("c");
      az.add("D");
      az.add("d");
      az.add("E");
      az.add("e");
      az.add("F");
      az.add("f");
      az.add("G");
      az.add("g");
      az.add("H");
      az.add("h");
      az.add("I");
      az.add("i");
      az.add("J");
      az.add("j");
      az.add("K");
      az.add("k");
      az.add("L");
      az.add("l");
      az.add("M");
      az.add("m");
      az.add("N");
      az.add("n");
      az.add("O");
      az.add("o");
      az.add("P");
      az.add("p");
      az.add("Q");
      az.add("q");
      az.add("R");
      az.add("r");
      az.add("S");
      az.add("s");
      az.add("T");
      az.add("t");
      az.add("U");
      az.add("u");
      az.add("V");
      az.add("v");
      az.add("W");
      az.add("w");
      az.add("X");
      az.add("x");
      az.add("Y");
      az.add("y");
      az.add("Z");
      az.add("z");    
}
}

Here is the html code I use to call my JApplet.

<applet code="http://tekhaxs.com/applet.java" width="400" height="400">
If your browser was Java-enabled, a Puush Browser would appear here.
</applet>

Any suggestions on how to fix this error?

You need to provide the class file of your applet in the code attribute:

<applet code="applet.class" width="400" height="400">

This should work if the class file is at the same location as your html file. If the class file is at a different location, you need to specify the location through an additional codebase attribute, eg if the class file is located in a bin subdirectory, specify

<applet code="applet.class" codebase="bin" width="400" height="400">

See http://www.duckware.com/applets/reference.html for additional information.

Essentially,

  • code refers to the class of the main applet class, including any package names, and with a .class postfix, like in code="com.example.SampleApplet.class" .
  • codebase is a URL (either relative or absolute) which refers to the location where the class file specified in code can be found. If it is the same location as the html file, codebase can be omitted.

now I am getting a different error.

Access denied ("java.net.SocketPermission""Puu.sh:80""connect,ressolve")

Your applet code does not have the necessary access rights to use sockets (which is required to access puu.sh which you do in your code). Note that applets are running on the client machine, and by default they are not allowed any access outside their sandbox.

You can adjust the privileges by creating a so-called policy file on the client machine - See http://download.java.net/jdk8/docs/technotes/guides/security/PolicyFiles.html for more information. Note that this needs to be done on the client side.

I would try to put the images on the same server where your applet resides. Then you should be able to download them without modifying the security policies.

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