用于加载applet的ClassNotFoundException

时间:2013-06-07 19:26:22

标签: java browser applet

我无法运行这个简单的嵌入式小程序。

java.lang.ClassNotFoundException:非常明确,但我认为每件事都没问题。

小程序代码

package org.test;

import java.applet.*;
import java.awt.*;

    public class Main extends Applet
    {
        public void init()
        {}

        public void stop()
        {}

        public void paint(Graphics g)
        {
            g.drawString("Salam Applet",20,20);
        }
    }

UI代码(jsp页面)

<html>
<head>
    <title>salam applet</title>
</head>
<body>
<applet code="org.test.Main" width="200" height="500"></applet>
</body>
</html>

我在运行时的错误是

Java Plug-in 1.6.0_23
Using JRE version 1.6.0_23-b05 Java HotSpot(TM) Client VM
User home directory = C:\Users\hardcode
----------------------------------------------------
c:   clear console window
f:   finalize objects on finalization queue
g:   garbage collect
h:   display this help message
l:   dump classloader list
m:   print memory usage
o:   trigger logging
q:   hide console
r:   reload policy configuration
s:   dump system and deployment properties
t:   dump thread list
v:   dump thread stack
x:   clear classloader cache
0-5: set trace level to <n>
----------------------------------------------------
load: class org.test.Main not found.
java.lang.ClassNotFoundException: org.test.Main
    at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.io.IOException: open HTTP connection failed:http://localhost:8080/org/test/Main.class
    at sun.plugin2.applet.Applet2ClassLoader.getBytes(Unknown Source)
    at sun.plugin2.applet.Applet2ClassLoader.access$000(Unknown Source)
    at sun.plugin2.applet.Applet2ClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    ... 9 more
Exception: java.lang.ClassNotFoundException: org.test.Main

这是浏览器中错误的图片。 enter image description here

1 个答案:

答案 0 :(得分:2)

让我们假设以下HTML位于localhost根目录的applet.html中。 http://localhost:8080/applet.html

<html>
<head>
    <title>salam applet</title>
</head>
<body>
<applet code="org.test.Main" width="200" height="500"></applet>
</body>
</html>

未指定时,代码库指向文档库http://localhost:8080/

由于未声明archive,因此JVM将期望在以下位置找到类文件:

http://localhost:8080/org/test/Main.class

此类路径的'酸性测试'是将其复制/粘贴到浏览器地址栏并点击 Enter


您也可以尝试使用Appleteer打开它,它会执行更多检查并生成更详细的输出。