Applet failed problem

Hi!

Im new to working with Java applets. I am going to describe my problem with the applet I have written and all the circumstances of it because maybe I have missed out some little detail that somebody can point out.

So, I was given the assignment of modifying my Professor's applet a little bit. The structure of the code was class example1 extends class table extends class BufferedApplet. I made the changes to the applet using Netbeans IDE 4.1. So the applet runs perfectly in the applet viewer, and the HTML page created by the netbeans on running the example1.java code also works fine if I open it from the build folder using firefox, or explorer.

So I thought, great, I got my assignment done. We must post the applet on our website for it to get graded. So I uploaded the example1.class file and the example1.html file to the same directory on the webpage. I added table.class and BufferedApplet.class as well, just to be safe. All three source files are also in the same directory, coz we have to provide links to them for the assignment.

Now, when I open the webpage, it wont display the applet. It shows me a white box with a red cross in the top left corner and gives me the message "loading java applet failed". sometimes, there are variations like "example not inited" or "java.lang.NullPointerException"... Here is what the Sun Java console had to say:

Java Plug-in 1.5.0_04

Using JRE version 1.5.0_04 Java HotSpot(TM) Client VM

User home directory = C:\Documents and Settings\Vidya at work

-

c:clear console window

f:finalize objects on finalization queue

g:garbage collect

h:displaythis help message

l:dump classloader list

m:print memory usage

o:trigger logging

p:reload proxy configuration

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>

-

java.lang.NoClassDefFoundError: example1 (wrong name: multimedia/example1)

at java.lang.ClassLoader.defineClass1(Native Method)

at java.lang.ClassLoader.defineClass(Unknown Source)

at java.security.SecureClassLoader.defineClass(Unknown Source)

at sun.applet.AppletClassLoader.findClass(Unknown Source)

at java.lang.ClassLoader.loadClass(Unknown Source)

at sun.applet.AppletClassLoader.loadClass(Unknown Source)

at java.lang.ClassLoader.loadClass(Unknown Source)

at sun.applet.AppletClassLoader.loadCode(Unknown Source)

at sun.applet.AppletPanel.createApplet(Unknown Source)

at sun.plugin.AppletViewer.createApplet(Unknown Source)

at sun.applet.AppletPanel.runLoader(Unknown Source)

at sun.applet.AppletPanel.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

Exception in thread"Thread-4" java.lang.NullPointerException

at sun.plugin.util.GrayBoxPainter.showLoadingError(Unknown Source)

at sun.plugin.AppletViewer.showAppletException(Unknown Source)

at sun.applet.AppletPanel.runLoader(Unknown Source)

at sun.applet.AppletPanel.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

java.lang.NullPointerException

at sun.plugin.util.GrayBoxPainter.showLoadingError(Unknown Source)

at sun.plugin.AppletViewer.showAppletStatus(Unknown Source)

at sun.applet.AppletPanel.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

Exception in thread"thread applet-example1.class" java.lang.NullPointerException

at sun.plugin.util.GrayBoxPainter.showLoadingError(Unknown Source)

at sun.plugin.AppletViewer.showAppletException(Unknown Source)

at sun.applet.AppletPanel.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

java.lang.NoClassDefFoundError: example1 (wrong name: multimedia/example1)

at java.lang.ClassLoader.defineClass1(Native Method)

at java.lang.ClassLoader.defineClass(Unknown Source)

at java.security.SecureClassLoader.defineClass(Unknown Source)

at sun.applet.AppletClassLoader.findClass(Unknown Source)

at java.lang.ClassLoader.loadClass(Unknown Source)

at sun.applet.AppletClassLoader.loadClass(Unknown Source)

at java.lang.ClassLoader.loadClass(Unknown Source)

at sun.applet.AppletClassLoader.loadCode(Unknown Source)

at sun.applet.AppletPanel.createApplet(Unknown Source)

at sun.plugin.AppletViewer.createApplet(Unknown Source)

at sun.applet.AppletPanel.runLoader(Unknown Source)

at sun.applet.AppletPanel.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

I have been trying to find solutions to this problem for weeks now.... Please help!!!!

Here is my html page....

<HTML>

<HEAD>

<TITLE>Modified Test Environment</TITLE>

</HEAD>

<BODY bgcolor=black text=white link=white alink=white vlink=white>

<!--

*** GENERATED applet HTML launcher - DO NOT EDIT IN'BUILD' FOLDER ***

If you need to modifythis HTML launcher file (e.g., to add applet parameters),

copy it to where your appletclass is found in the SRC folder. If youdo this,

the IDE will use it when you run or debug the applet.

Tip: To exclude an HTML launcher from the JAR file, use exclusion filters in

the Packaging page in the Project Properties dialog.

For more information see the online help.

-->

<H3>Modified Test Environment</H3>

<center>

<APPLET codebase="." code="example1.class" width=500 height=500></APPLET>

</center>

<P>

Source: <a href=example1.java>example1</a>extends <a href=table.java> table</a>extends <a href=BufferedApplet.java>BufferedApplet</a>

</P>

</BODY>

</HTML>

??

[6353 byte] By [vpaia] at [2007-10-2 0:29:35]
# 1

I think that you have a class called example1 and the source code for example1 starts with the line, "package multimedia;" This means that the example1.class file must be in a directory called multimedia. The multimedia directory should be in the same directory as the HTML file. And the applet tag should be <APPLET codebase="." code="multimedia.example1.class" width=500 height=500></APPLET>

atmguya at 2007-7-15 16:44:02 > top of Java-index,Administration Tools,Sun Connection...
# 2

That makes sense. Ok, so now i made those changes, but the applet still does not work... It gives me the error that multimedia.example1 not inited or something like that. the ClassDefNotFound error has gone away, but ti now ha the error reports as follows in the sun java console:

h:display this help message

l:dump classloader list

m:print memory usage

o:trigger logging

p:reload proxy configuration

q:hide console

r:reload policy configuration

s:dump system properties

t:dump thread list

v:dump thread stack

x:clear classloader cache

0-5: set trace level to <n>

-

load: class multimedia.example1.class not found.

java.lang.ClassNotFoundException: multimedia.example1.class

at sun.applet.AppletClassLoader.findClass(Unknown Source)

at java.lang.ClassLoader.loadClass(Unknown Source)

at sun.applet.AppletClassLoader.loadClass(Unknown Source)

at java.lang.ClassLoader.loadClass(Unknown Source)

at sun.applet.AppletClassLoader.loadCode(Unknown Source)

at sun.applet.AppletPanel.createApplet(Unknown Source)

at sun.plugin.AppletViewer.createApplet(Unknown Source)

at sun.applet.AppletPanel.runLoader(Unknown Source)

at sun.applet.AppletPanel.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

Caused by: java.io.IOException: open HTTP connection failed.

at sun.applet.AppletClassLoader.getBytes(Unknown Source)

at sun.applet.AppletClassLoader.access$100(Unknown Source)

at sun.applet.AppletClassLoader$1.run(Unknown Source)

at java.security.AccessController.doPrivileged(Native Method)

... 10 more

vpaia at 2007-7-15 16:44:02 > top of Java-index,Administration Tools,Sun Connection...
# 3
here's the link to the page where i have tried to upload the applet. the source code is also linked to it... i'd be very grateful if the bug could be found... http://homepages.nyu.edu/~vp473/applet1.html
vpaia at 2007-7-15 16:44:02 > top of Java-index,Administration Tools,Sun Connection...
# 4
I looked at the html code of your page. In the html Applet line, it says codebase=.. so the jvm is expecting to find a multimedia directory one directory up from the directory that holds the html file. Is that where it is and is the .class file inside the multimedia directory?
atmguya at 2007-7-15 16:44:02 > top of Java-index,Administration Tools,Sun Connection...
# 5
Yup. Altered it so my html now says codebase=".", but still failing in loading the applet. says not inited... :(
vpaia at 2007-7-15 16:44:02 > top of Java-index,Administration Tools,Sun Connection...
# 6

What does it mean when the console says noClassDefFound: wrong name? Here is the output from the console:

java.lang.NoClassDefFoundError: multimedia/table (wrong name: table)

at java.lang.ClassLoader.defineClass1(Native Method)

at java.lang.ClassLoader.defineClass(Unknown Source)

at java.security.SecureClassLoader.defineClass(Unknown Source)

at sun.applet.AppletClassLoader.findClass(Unknown Source)

at java.lang.ClassLoader.loadClass(Unknown Source)

at sun.applet.AppletClassLoader.loadClass(Unknown Source)

at java.lang.ClassLoader.loadClass(Unknown Source)

at java.lang.ClassLoader.loadClassInternal(Unknown Source)

at java.lang.ClassLoader.defineClass1(Native Method)

at java.lang.ClassLoader.defineClass(Unknown Source)

at java.security.SecureClassLoader.defineClass(Unknown Source)

at sun.applet.AppletClassLoader.findClass(Unknown Source)

at java.lang.ClassLoader.loadClass(Unknown Source)

at sun.applet.AppletClassLoader.loadClass(Unknown Source)

at java.lang.ClassLoader.loadClass(Unknown Source)

at sun.applet.AppletClassLoader.loadCode(Unknown Source)

at sun.applet.AppletPanel.createApplet(Unknown Source)

at sun.plugin.AppletViewer.createApplet(Unknown Source)

at sun.applet.AppletPanel.runLoader(Unknown Source)

at sun.applet.AppletPanel.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

Exception in thread "Thread-4" java.lang.NullPointerException

at sun.plugin.util.GrayBoxPainter.showLoadingError(Unknown Source)

at sun.plugin.AppletViewer.showAppletException(Unknown Source)

at sun.applet.AppletPanel.runLoader(Unknown Source)

at sun.applet.AppletPanel.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

java.lang.NullPointerException

at sun.plugin.util.GrayBoxPainter.showLoadingError(Unknown Source)

at sun.plugin.AppletViewer.showAppletStatus(Unknown Source)

at sun.applet.AppletPanel.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

Exception in thread "thread applet-multimedia/example1.class" java.lang.NullPointerException

at sun.plugin.util.GrayBoxPainter.showLoadingError(Unknown Source)

at sun.plugin.AppletViewer.showAppletException(Unknown Source)

at sun.applet.AppletPanel.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

vpaia at 2007-7-15 16:44:02 > top of Java-index,Administration Tools,Sun Connection...
# 7

All Java classes have a fully qualified class name. Fully qualified means the package name plus the class name. The wrong name error means that the jvm found a .class file with a suitable name, but inside the file was a class whose fully qualified name does not match.

In your post with "wrong name: table" the class loader was probably looking for a class named multimedia.table but it found plain table. You probably do not have a "package multimedia;" in the table.java code.

atmguya at 2007-7-15 16:44:02 > top of Java-index,Administration Tools,Sun Connection...
# 8
None of the .java files are in a package. You can see the package stmts commented out in the source code links on the page...
vpaia at 2007-7-15 16:44:03 > top of Java-index,Administration Tools,Sun Connection...
# 9
You must have made a change since your first post because the error message clearly states that the jvm found a class whose name was multimedia/example1. If your classes are not in a named package, then those classes should be in the same directory as the html file.
atmguya at 2007-7-15 16:44:03 > top of Java-index,Administration Tools,Sun Connection...
# 10

ok here is my directory structure... i have put all three .class files in two places: directly in the same directory as applet.html, as well as in a folder called multimedia which is in the same directory as applet.html. I made the copies jsut coz i wasnt sure where they belonged, so i tried both. the current applet file is set to find the example.class file in the multimedia folder. but i have not changed any of the source code, there is no packaging in the source code...

So we have folder named "public" containing:

- example1.java

-example1.class

-tabel.java

-table.class

-BufferedApplet.java

-BufferedApplet.class

-applet.html

-folder named "multimedia"

the folder named multimedia contains:

-example1.java

-example1.class

-table.java

-table.class

-BufferedApplet.java

-BufferedApplet.class

Thanx a ton for taking the effort to reply to all of this... I have been running from pillar to post to find this very evasive solution... im sure it is something really trivial at the end of it all... just hope i find it!

vpaia at 2007-7-15 16:44:03 > top of Java-index,Administration Tools,Sun Connection...
# 11

You are probably causing yourself problems by having the .class files in two places. Java generally searches until it finds a suitable .class file, not necessarily the best match.

You may benefit by reading this part of an Applet tutorial. http://java.sun.com/docs/books/tutorial/applet/overview/test.html

Basically, the codebase specifies the directory which is the root for finding .class files - like the Classpath. From the root directory, you must have a directory structure which matches the package name. If none of your classes have a named package, then all of your classes should be in the codebase directory. (BTW, codebase defaults to .)

The code tag specifies a fully qualified class name. Fully qualified means the package name plus the class name. If you specify "multimedia/example1" then Java looks for an example1.class file inside a multimedia directory and multimedia must exist in the codebase directory. If none of your classes have a named package, then you should not specify a package name in the code tag.

To summarize, codebase is the root directory for classes; .class files must be in a directory structure that matches their package name; the code tag specifies a fully qualified class name.

So, your html code tag must be changed to code="example1.class" no matter where you put the .class file because example1 does not have a named package.

You can put the files in the same directory as the html file or in the multimedia directory. Just change the codebase tag to reflect where they are.

atmguya at 2007-7-15 16:44:03 > top of Java-index,Administration Tools,Sun Connection...