【问题标题】:Cant run JApplet on html, ClassNotFoundException无法在 html 中运行 Applet,ClassNotFoundException
【发布时间】:2014-07-21 18:19:18
【问题描述】:

我写了一个简单的 JApplet,我想在我的网站上运行它。我将它打包成一个 .jar 文件并在 html 上使用 applet 标记。这是我的html:

<html>
<applet code="main.class" 
codebase="test/"
archive="tmp.jar"
width="600" height="95">
<param name="type" value="hello">
<param name="IP" value="127.0.0.1">
</html>

这是我的 main.java:

public class main extends JApplet{
    public String str, IP;
    public ImagePanel panel;
    protected void loadAppletParameters(){
         String at = getParameter("type");
         str = (at != null) ? at : "world";
         at = getParameter("IP");
         IP = (at != null) ? at : "127.0.0.1";
    }
    public void init(){
        loadAppletParameters();
        System.out.println("hi: ");
        panel = new ImagePanel();
        this.add(panel);

    }

    public class ImagePanel extends JPanel{

        private BufferedImage image;

        public ImagePanel() {
           try {                
              image = ImageIO.read(new File("img.jpg"));
           } catch (IOException e) {
               e.printStackTrace();
           }
        }
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(image, 0, 0, null);           
        }
        public void change_image(String path){
            try {
                image = ImageIO.read(new File(path));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}    

但是当我打开 web 文件时它捕获了 ClassNotFoundException。请帮助或给我一些建议。非常感谢。

【问题讨论】:

  • 您是否检查了 Java 控制台的详细错误信息?
  • 我检查过了,没有错误。
  • 您是否没有想到,HTML 和tmp.jar 的目录结构和位置对于解决这个问题很重要?请edit the question 包含该信息。顺便说一句 - 1) g.drawImage(image, 0, 0, null); 应该是 g.drawImage(image, 0, 0, this);,因为每个 JComponent 是一个 ImageObserver。 2)image = ImageIO.read(new File("img.jpg"));applet 的资源需要加载为URL,而不是File。最好是相对于 HTML 或文档库中指定的 codebase 形成的 URL

标签: java html applet classnotfoundexception japplet


【解决方案1】:

尝试编译.java文件得到.class文件并粘贴到test/中。检查它是否有字。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-05
    • 1970-01-01
    • 2015-02-05
    • 2017-01-12
    • 1970-01-01
    • 2014-10-01
    相关资源
    最近更新 更多