0

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

Ammar Bozorgvar
  • 1,230
  • 19
  • 30
  • What is the the URL in the address bar when you visit the applet page? Note that an applet will almost certainly need to be packaged in a Jar if deployed to real world clients. – Andrew Thompson Jun 07 '13 at 19:29
  • i did not try to do that, but i can see this error "Caused by: java.io.IOException: open HTTP connection failed:http://localhost:8080/org/test/Main.class" – Ammar Bozorgvar Jun 07 '13 at 19:31
  • **Copy** the URL in the address bar for me. **Paste** it here. I need a base to make a point. – Andrew Thompson Jun 07 '13 at 19:34
  • BTW 1) Why code an applet? If it is due due to spec. by teacher, please refer them to [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why AWT rather than Swing? See this answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. If you need to support older AWT based APIs, see [Mixing Heavyweight and Lightweight Components](http://www.oracle.com/technetwork/articles/java/mixing-components-433992.html). – Andrew Thompson Jun 07 '13 at 19:39
  • how can i find the address ? – Ammar Bozorgvar Jun 07 '13 at 19:42

1 Answers1

2

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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • i build the project, create a war file, droped it in my tomcat, i copyed the address "http://localhost:8080/firstApplet/org/test/Main.class", and i see error 404 ?! – Ammar Bozorgvar Jun 07 '13 at 20:13