小程序异常:java.lang.ClassNotFoundException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11835599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 06:34:19  来源:igfitidea点击:

Applet exception: java.lang.ClassNotFoundException

javahtmlapplet

提问by willbattel

I have a folder with two files inside; the java .class file and the .html file. In my html file I call the .class file as an applet but it sais an error on the website its published to saying it can't find the .class file. This puzzles me since they are in the same directory and I triple checked for spelling errors.

我有一个文件夹,里面有两个文件;java .class 文件和 .html 文件。在我的 html 文件中,我将 .class 文件称为小程序,但它在发布的网站上说一个错误,说它找不到 .class 文件。这让我很困惑,因为它们在同一个目录中,我三重检查了拼写错误。

Here is my .html file...

这是我的 .html 文件...

<html>

<head>
<title>Applet</title>
</head>

<body>
Program<br />
<applet code="testing.class" width="300" height="300"/>
</body>

</html>

and here is my .class file...

这是我的 .class 文件...

import java.awt.Color;
import java.awt.Graphics;


public class testing extends java.applet.Applet{

    public void init(){

    }

    public void paint(Graphics g){
        g.drawOval(0,0,250,100);
        g.setColor(Color.RED);
        g.drawString("My Applet",10,50);
    }

}

My .class file is "testing.class" and my html file is "testingpage.html"

我的 .class 文件是“testing.class”,我的 html 文件是“testingpage.html”

Here is the Full Entire error

这是完整的整个错误

load: class testing.class not found.
java.lang.ClassNotFoundException: testing.class
    at sun.plugin2.applet.Applet2ClassLoader.findClass(Applet2ClassLoader.java:252)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Plugin2ClassLoader.java:249)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Plugin2ClassLoader.java:179)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Plugin2ClassLoader.java:160)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Plugin2ClassLoader.java:690)
    at sun.plugin2.applet.Plugin2Manager.createApplet(Plugin2Manager.java:3045)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Plugin2Manager.java:1497)
    at java.lang.Thread.run(Thread.java:680)
Exception: java.lang.ClassNotFoundException: testing.class
load: class testing.class not found.
java.lang.ClassNotFoundException: testing.class
    at sun.plugin2.applet.Applet2ClassLoader.findClass(Applet2ClassLoader.java:252)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Plugin2ClassLoader.java:249)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Plugin2ClassLoader.java:179)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Plugin2ClassLoader.java:160)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Plugin2ClassLoader.java:690)
    at sun.plugin2.applet.Plugin2Manager.createApplet(Plugin2Manager.java:3045)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Plugin2Manager.java:1497)
    at java.lang.Thread.run(Thread.java:680)
Exception: java.lang.ClassNotFoundException: testing.class

回答by oldrinb

The applettag should be:

applet标签应该是:

<applet code=testing.class width="300" height="300" />

Notice the change in the codeattribute. Compare to the example code listed in the relevant Java Tutorial:

注意code属性的变化。与相关 Java 教程中列出的示例代码进行比较:

<applet code=Applet1.class width="200" height="200">
Your browser does not support the <code>applet</code> tag.
</applet> 

The following is working for me.

以下是对我来说有效。

http://puu.sh/PebS

http://puu.sh/PebS

TestingApplet.java

测试Applet.java

import java.applet.Applet;

import java.awt.Color;
import java.awt.Graphics;

public final class TestingApplet extends Applet {

  public void paint(final Graphics g){
    g.drawOval(0, 0, 250, 100);
    g.setColor(Color.RED);
    g.drawString("My Applet", 10, 50);
  }
}

testing-applet.html

测试applet.html

<html>
  <head>
    <title>Applet</title>
  </head>
  <body>
    Program <br />
    <applet code=TestingApplet.class width="300" height="300" />
  </body>
</html>

If this is not working, I have two questions for you...

如果这不起作用,我有两个问题要问你......

  • Did you save the Java as a .javafile and compile it to produce the correct .classfile?

  • Did you verify your browser is not caching an old incorrect version of the .htmlfile?

  • Are your .classand .htmlfiles in the same directory?

  • 您是否将 Java 保存为.java文件并编译它以生成正确的.class文件?

  • 您是否确认您的浏览器没有缓存旧的错误版本的.html文件?

  • .class.html文件在同一个目录下吗?

回答by GoodLuck

I am pretty sure that you're not openning the correct HTML file. Use your File Browser(Finder I guess ;) ) and look for the file in the Netbeans project directory. It should be in /build/classes/.html

我很确定您没有打开正确的 HTML 文件。使用您的文件浏览器(我猜是 Finder ;))并在 Netbeans 项目目录中查找该文件。它应该在 /build/classes/.html