Well first of all, please let me say how nice this website is - I'm very pleased to have found it and plan to be sticking around while I'm learning Java as part of my Uni course.
I think I will probably get off to pretty bad start here, by asking a question that is almost certainly a 'Frequently Asked Question'. In my defence, I have looked at quite a few sites on the net and not found the answer.
First, I'll set the scenario...
I'm running java on Linux. My $CLASSPATH variable is set correctly to "/home/jpw48/public_html/java/classes". I have made myself a package which we'll call myPackage. This contains the class Jack.class , so the full location of Jack.class is "/home/jpw48/public_html/java/classes/myPackage/Jack.class". Now, I have 'local' directory, which contains the file Jill.class at "/home/jpw48/java/jill/Jill.class". Jill imports Jack using the statement "import myPackage.Jack;".
Still with me?
Now, Jill extends JApplet, so to run Jill, I have to make Jill.html, which includes the line:
I use the command "appletviewer Jill.html" to run Jill, but it doesn't work, complaining of a NoClassDefFoundError at runtime. Clearly, it doesn't know where to find Jack. After some trawling through the internet, I found a way to solve this problem by giving the command:
appletviewer -J-classpath -J$CLASSPATH Jill.html
And this works fine. So what's the problem? The problem is if I try to open Jill.html with my normal browser like this...
firefox Jill.html &
... it doesn't work for the same reason as why appletviewer didn't work initially - it doesn't know the classpath. There's no command line parameter I can give to firefox to tell it the classpath; it's got to be put into the html file somehow, I reckon.
Thanks guys, I look forward to hearing your response(s).
You're right, the browser will not honor your CLASSPATH, so everything needs to be specified in the applet tag. (There are other places a JVM will look for classes, but for the purpose of learning Java you should ignore those.) And it gets worse: In an applet tag you can specify only a single directory where the JVM will look for classes. Theoretically it might be possible to use symbolic links to combine various directories into a single one, but I'm not sure that the JVM can handle links. So for the moment I would suggest that you create a single directory containing all your classes, and copy files over to the public directory as needed.
The Applet FAQ, which is linked in my signature, may be useful in learning more about applets. Its second answer has some links to more information about the applet tag.
Ping & DNS - updated with new look and Ping home screen widget
Joined: Mar 16, 2006
Hi Ulf, thanks for your reply. I gather that it's a fairly tricky topic getting applets to import your own custom classes.
So for the moment I would suggest that you create a single directory containing all your classes, and copy files over to the public directory as needed.
Is this what you would do? If there is a more advanced but better way of doing it, I would very much like to know (even if it is too advanced for me to understand - it will provide me with a starting point). Thanks.
That's pretty much what you always do. In the "real world" applets are never used except in a remote Web browser arrangement -- i.e., all the classes come over the network. In that case, you wouldn't expect your local CLASSPATH to have any effect, right?
By the way, the very best advice any Java newbie can get is not to set the CLASSPATH to anything -- ensure, in fact, that it is unset. If it's currently set, unset it and leace it that way. Then, when necessary, use a -classpath switch to the compiler or runtime. For applets, do the one-directory thing.
Reliance on CLASSPATH always leads to the "But it worked on my machine, why doesn't it work on yours!" problem which drives people nuts. Keep everything explicit, and you'll always know what's going on.