File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Applets and the fly likes the applet and the $CLASSPATH Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "the applet and the $CLASSPATH" Watch "the applet and the $CLASSPATH" New topic
Author

the applet and the $CLASSPATH

John Wickerson
Greenhorn

Joined: Mar 16, 2006
Posts: 13
Hey guys,

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:

<APPLET code="Jill.class" width="1000" height="800">

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.

How?

Thanks guys, I look forward to hearing your response(s).

Kind regards,
John
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41489
    
  53
Welcome to JavaRanch.

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 - my free Android networking tools app
John Wickerson
Greenhorn

Joined: Mar 16, 2006
Posts: 13
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.


Kind regards,
John
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41489
    
  53
For applets, there is no better way of organizing the classes than to put them in a single directory, as Ernest pointed out. That's indeed what I would do.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: the applet and the $CLASSPATH