简体   繁体   English

JApplet ClassNotFoundException

[英]JApplet ClassNotFoundException

I am currently trying to turn my JFrame into a JApplet. 我目前正在尝试将我的JFrame变成JApplet。

It runs fine in eclipse as an applet but when I try to use it on my website it gives me an error. 它在eclipse中作为小程序运行良好,但是当我尝试在我的网站上使用它时,它给我一个错误。

Here is my Applet: http://tekhaxs.com/applet.java 这是我的Applet: http : //tekhaxs.com/applet.java

You can view my java source there ^^ or below. 您可以在^^或以下查看我的Java源代码。

here is the error: http://tekhaxs.com/?page_id=146 这是错误: 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. 这是我用来调用JApplet的html代码。

<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: 您需要在code属性中提供小程序的类文件

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

This should work if the class file is at the same location as your html file. 如果类文件与html文件位于同一位置,则此方法应该起作用。 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 如果类文件位于其他位置,则需要通过其他codebase属性指定位置,例如,如果类文件位于bin子目录中,请指定

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

See http://www.duckware.com/applets/reference.html for additional information. 有关其他信息,请参见http://www.duckware.com/applets/reference.html

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" . code是指applet主类的类,包括任何程序包名称,并带有.class后缀,如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. codebase是一个URL(相对的或绝对的),它是指可以找到code指定的类文件的位置。 If it is the same location as the html file, codebase can be omitted. 如果与html文件位于同一位置,则可以省略codebase

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). 您的applet代码没有使用套接字的必要访问权限(访问在代码中执行的puu.sh是必需的)。 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. 您可以通过在客户端计算机上创建所谓的策略文件来调整特权-有关更多信息,请参见http://download.java.net/jdk8/docs/technotes/guides/security/PolicyFiles.html 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. 然后,您应该能够下载它们而无需修改安全策略。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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