Ammar Bozorgvar
Ammar Bozorgvar

Reputation: 1278

ClassNotFoundException for loading the applet

I can't run this simple embedded applet.

the java.lang.ClassNotFoundException: is very clear, but I think every thing is OK.

applet code

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 code (jsp page)

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

and my error at run time is

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

this is the picture of the error in the browser. enter image description here

Upvotes: 0

Views: 5627

Answers (1)

Andrew Thompson
Andrew Thompson

Reputation: 168845

Let us imagine that the following HTML is in applet.html at the root of localhost. 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>

The code base, when not specified, points to the document base http://localhost:8080/.

Since no archive is declared, the JVM will expect the class file to be found at:

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

An 'acid test' for such a path is to copy/paste it into a browser address bar and hit Enter


You might also try opening it with Appleteer, which does more checks and produces more detailed output.

Upvotes: 2

Related Questions