View Full Version : Coding Help
saxon75
10-22-2003, 06:05 PM
Hey programming-types. I'm trying to learn to write Java applets and can't seem to get it right. In fact, I can't even get a Hello World applet to function. Anyone out there with Java experience?
Here's the code:
public class HelloWorld extends Applet {
public void paint(Graphics g) {
g.drawString("Hello world!", 50, 25);
}
}
And here's the error I get when I try to execute it in NetBeans:
java.lang.NoClassDefFoundError: Msakaseg/Personal/Temp/HelloWorld (wrong name: HelloWorld)
at java.lang.ClassLoader.defineClass0( Native Method)
at java.lang.ClassLoader.defineClass(C lassLoader.java:537)
at java.security.SecureClassLoader.def ineClass(SecureClassLoader.java:123 )
at sun.applet.AppletClassLoader.findCl ass(AppletClassLoader.java:157)
at java.lang.ClassLoader.loadClass(Cla ssLoader.java:289)
at sun.applet.AppletClassLoader.loadCl ass(AppletClassLoader.java:123)
at java.lang.ClassLoader.loadClass(Cla ssLoader.java:235)
at sun.applet.AppletClassLoader.loadCo de(AppletClassLoader.java:561)
at sun.applet.AppletPanel.createApplet (AppletPanel.java:617)
at sun.applet.AppletPanel.runLoader(Ap pletPanel.java:546)
at sun.applet.AppletPanel.run(AppletPa nel.java:298)
at java.lang.Thread.run(Thread.java:53 4)
Xavier
10-23-2003, 03:05 AM
w00t, I finally get to a programming question before someone else does. It just so happens that Java is my specialty, even if I haven't done applets in a while, this one is easy.
The first thing I notice is that you have no import statements. In order for your applet to run, you need to put this at the top:
import java.awt.*;
import java.applet.*;
Thats not what is causing your problem though. That error message means that there is a naming problem usually. Make sure that the file is called HelloWorld.java. But, it might be that you are trying to execute a class which has no main method. Applets don't run like programs do, they are used on web-pages like this:
<html>
<APPLET CODE=HelloWorld.class WIDTH=300 HEIGHT=100>
</APPLET>
</html>
So if you put that html file in the same directory as your java class, it should run fine when looking at the web-page (mine does when I tested it). I'm not sure what NetBeans does upon execute, but make sure you are compiling the class using javac.
If you want to avoid making the webpage to test it, there are programs which test applets, I think Appletviewer even comes with the java SDK. I haven't used it in years though, so don't ask me how to use it :). Also, there is a great tutorial off of Sun Microsystems website you can use for reference.
Xavier
10-23-2003, 03:08 AM
Actually if you want to make it super-efficient, the import statements:
import java.applet.Applet;
import java.awt.Graphics;
would be better.
If you're a monkey, I'm something like a brontosaurus.
saxon75
10-23-2003, 12:22 PM
Actually, I did include the import statements, I just forgot to include them in my post. When you execute an applet in NetBeans, it first compiles the code, then launches Appletviewer, which generates an html file to embed the applet in and then views it.
The code above (plus import statements) is in a file called HelloWorld.java, and when compiled produces a file called HelloWorld.class. I wrote my own html file before I discovered the Appletviewer containing the following code:
<html>
<head>
<title>A Simple Java Applet</title>
</head>
<body>
Here is the output of my program:
<applet code="HelloWorld.class" width=150 height=25>
</applet>
</body>
</html>
But when I view the page it just gives me a blank gray box and a message in the status bar that says "load: class HelloWorld not found".
::sigh::
Zevlag
10-23-2003, 12:23 PM
::irrelevant::
Xavier
10-23-2003, 04:08 PM
Okay then, the only thing I can think of is that your html file isn't finding the class file, which would happen if they weren't in the same directory. Either that or your protections need to be changed. The class file probably has to be at least executable.
Not sure what else to say, it works fine on this end :(.
saxon75
10-24-2003, 02:06 PM
I definitely have the class file in the same directory, and in windows there shouldn't be a problem with permissions. I tried uploading it to my web server and giving it rwx|r_x|r_x permissions, but it still doesn't work. I'm starting to think there's something wrong with my compiler.
Can you compile it for me and email me the class file?
saxon75
10-24-2003, 03:38 PM
Never mind I got it now. It was some combination of my compiler and my VM.
It was a matter of classpath's, I bet? That's always my problem :)
-Bok
PS- Is my title custom or generic? It's strange in any event.
Zevlag
10-27-2003, 10:23 PM
No, it's custom, our good Leto The Great has taken to setting them when he knows/sees a little about an individual.
vBulletin® v3.6.8, Copyright ©2000-2012, Jelsoft Enterprises Ltd.