简体   繁体   English

Appletviewer引发java.lang.NoClassDefFoundError异常

[英]Appletviewer throws java.lang.NoClassDefFoundError exception

I want to run an applet in appletviewer . 我想在appletviewer运行一个applet My project directory has the following structure: 我的项目目录具有以下结构:

/home/sanctus/workspace/AppletDocumentLoader/bin/com/examples/ti

AppletDocumentLoader is my project, then in /bin/com/examples/ti are 3 class files and the HTML file ie. AppletDocumentLoader是我的项目,然后在/bin/com/examples/ti有3个类文件和HTML文件,即。 all in the same directory. 全部在同一个目录中。

My src class 我的src班

package com.examples.ti;
import java.applet.AppletContext;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

public class Applet extends JApplet {

    private static final long serialVersionUID = -8756947240188460854L;


    public void init() {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {

                @Override
                public void run() {
                    JLabel label = new JLabel("Hello World");
                    add(label);
                    JButton openDocument = new JButton();
                    openDocument.setText("Button");
                    openDocument.addActionListener(new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent e) {
                            System.out.println("BUTTON CLICKED");
                            AppletContext appletContext = getAppletContext();
                            try {
                                appletContext
                                        .showDocument(
                                                new URL(
                                                        "http://www.google.com"),
                                                "_self");
                            } catch (MalformedURLException e1) {
                                e1.printStackTrace();
                            }

                        }
                    });
                    add(openDocument);
                }
            });
        } catch (InvocationTargetException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}

My HTML file 我的HTML档案

<!DOCTYPE html>
<html>
<head>
  <title>Hi there</title>
</head>
<body>
  This is a page
  a simple page
</body>
<applet code="Applet.class" 
        archive="Applet.jar"
        width=350 height=350>
</applet>
</html>

And the error I get: 我得到的错误是:

sanctus@sanctus-desktop:~$ appletviewer '/home/sanctus/workspace/AppletDocumentLoader/bin/com/examples/ti/index.html' 
java.lang.NoClassDefFoundError: Applet (wrong name: com/examples/ti/Applet)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at sun.applet.AppletClassLoader.findClass(AppletClassLoader.java:217)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.applet.AppletClassLoader.loadClass(AppletClassLoader.java:152)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at sun.applet.AppletClassLoader.loadCode(AppletClassLoader.java:626)
    at sun.applet.AppletPanel.createApplet(AppletPanel.java:799)
    at sun.applet.AppletPanel.runLoader(AppletPanel.java:728)
    at sun.applet.AppletPanel.run(AppletPanel.java:378)
    at java.lang.Thread.run(Thread.java:745)

The code parameter of the applet tag should be the full name of the Java class, hence com.examples.ti.Applet . applet标记的code参数应为Java类的全名,即com.examples.ti.Applet See Applet code tags and class files 查看Applet代码标签和类文件

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

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