Hi
I have a sorting applet that implements Bubble Sort and Insertion sort, and i have a couple of classes in my program. I made it using netbeans and i have no main class, and when i run it using this code:

<html>
<applet code=Sorter.class
        width=800 height=300>
</applet>
</html>

I get the following

java.lang.NoClassDefFoundError: Sorter (wrong name: sort2/Sorter)
	at java.lang.ClassLoader.defineClass1(Native Method)
	at java.lang.ClassLoader.defineClass(Unknown Source)
	at java.security.SecureClassLoader.defineClass(Unknown Source)
	at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
	at java.lang.ClassLoader.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)
Exception: java.lang.NoClassDefFoundError: Sorter (wrong name: sort2/Sorter)

It is in a package called sort2 in netbeans, i just wondered whats going wrong.

Cheers
Paul

Recommended Answers

I think you need to add a CODE BASE parameter with a value that specifies where your .class file is located.

Without it, I don't think the AppletViewer can locate your .class file. I don't know enough about the JDK to verify this, but I do recall having a …

Jump to Post

If Main.class is declared in a package named "sort2" and in a folder of that same name, then from the root directory (the one containing the sorts directory), you invoke the class as "sort2.Main". The package is part of locating the class.
This tutorial may be helpful:

Jump to Post

All 8 Replies

I think you need to add a CODE BASE parameter with a value that specifies where your .class file is located.

Without it, I don't think the AppletViewer can locate your .class file. I don't know enough about the JDK to verify this, but I do recall having a similar problem after trying to make an Applet run on a different server.

So now my file looks like this:

<html>
<applet code="Sorter"
        width="800" height="300"
        CODEBASE="C:\Documents and 

Settings\Paul\My 

Documents\NetBeansProjects\Sort2\build

\classes\Sort2\">
</applet>
</html>

But i still get this error

oad: class Sorter not found.
java.lang.ClassNotFoundException: Sorter
	at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
	at java.lang.ClassLoader.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.FileNotFoundException: C:\Documents and Settings\Paul\My Documents\NetBeansProjects\Sort2\build\classes\sort2\Sorter.class (The system cannot find the file specified)
	at java.io.FileInputStream.open(Native Method)
	at java.io.FileInputStream.<init>(Unknown Source)
	at java.io.FileInputStream.<init>(Unknown Source)
	at sun.net.www.protocol.file.FileURLConnection.connect(Unknown Source)
	at sun.net.www.protocol.file.FileURLConnection.getInputStream(Unknown Source)
	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)
	... 7 more
Exception: java.lang.ClassNotFoundException: Sorter

I am so confused right now, it works fine in the netbeans applet viewer

Try it with your code base one level higher, like this

<html>
<applet code="Sorter"
        width="800" height="300"
        [B]CODEBASE="C:\Documents and Settings\Paul\My Documents\NetBeansProjects\Sort2\build\classes">[/B]
</applet>
</html>

It looks like you had the package directory in the previous code base.

Yeah now i get this:

load: class Main not found.
java.lang.ClassNotFoundException: Main
	at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
	at java.lang.ClassLoader.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.FileNotFoundException: C:\Documents and Settings\Paul\My Documents\NetBeansProjects\Sort2\build\classes\Main.class (The system cannot find the file specified)
	at java.io.FileInputStream.open(Native Method)
	at java.io.FileInputStream.<init>(Unknown Source)
	at java.io.FileInputStream.<init>(Unknown Source)
	at sun.net.www.protocol.file.FileURLConnection.connect(Unknown Source)
	at sun.net.www.protocol.file.FileURLConnection.getInputStream(Unknown Source)
	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)
	... 7 more
Exception: java.lang.ClassNotFoundException: Main

But when i put the html file in the same directory as the classes then i get this error

java.lang.NoClassDefFoundError: Main (wrong name: sort2/Main)
	at java.lang.ClassLoader.defineClass1(Native Method)
	at java.lang.ClassLoader.defineClass(Unknown Source)
	at java.security.SecureClassLoader.defineClass(Unknown Source)
	at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
	at java.lang.ClassLoader.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)
Exception: java.lang.NoClassDefFoundError: Main (wrong name: sort2/Main)

Oh and by the way i renamed the class main, and refactored it so it is still all working in netbeans.

Just wondering, if no-one can solve this where should i go for help? Is there a specific java forum somewhere on the web that would be able to help me with this?

Is your Main class in a folder called "sort2" and does it have a "package sort2;" declaration?

If Main.class is declared in a package named "sort2" and in a folder of that same name, then from the root directory (the one containing the sorts directory), you invoke the class as "sort2.Main". The package is part of locating the class.
This tutorial may be helpful: http://java.sun.com/docs/books/tutorial/java/package/index.html

commented: Couldnt have been a better post +2

WWOOOOOO!!! Thankyou! That is perfect!

<APPLET codebase="classes" code="sort2/Main.class" width=800 height=300></APPLET>

That is the code that worked in the end. Gosh, i am so happy now. Thanks for all your help Ezzaral.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.