Link to home
Start Free TrialLog in
Avatar of MFos239
MFos239

asked on

Using classes in a java applet that resides outside of the classpath

Why am I unable to declare and use classes (that reside in files in my classpath) in my Java applet (a file that resides outside of the classpath)?  I can do this if my Java applet source file resides in the classpath, however, if it is outside of the classpath I have problems.

My src file has this in it:

import com.backend.db.*;

and

Database db = new Database();

I can compile my code using the following:

javac -classpath c:\web\WEB-INF\classes c:\web\gui\testapplet.java

There is no error on compile.  When I attempt to run my applet in a browser, I get an error "Start: applet not initialized" and consequently the applet does not display.  If I remove my class declaration compiles, runs, and displays without error.  I know it's not a problem with the class itself because I am able to use the class in other Java code (that resides inside of the classpath).  Also I get the same initialization error problem if I try to use other classes (that reside in the classpath) in my applet (that resides outside the classpath).

Is there some special way you have to declare classes when your source code is outside of the classpath?  Or is there some special way to compile the code?  What am I doing wrong? It seems like this shouldn't be so hard to do.
Avatar of rrz
rrz
Flag of United States of America image

What does your Java Console print out?
Avatar of MFos239
MFos239

ASKER

When the applet tries to initialize I get this error in the console:

java.lang.NoClassDefFoundError: com/backend/db/Database
     at testapplet.init(testapplet.java:163)
     at sun.applet.AppletPanel.run(Unknown Source)
     at java.lang.Thread.run(Unknown Source)


Line 163 contains the code:

Database db = new Database();
If Database is not in your classpath then your applet create a Database object. To use the Database class it must be in the classpath.
Avatar of MFos239

ASKER

Database is in the classpath
Sorry what classpath are u referring to?
Where is the file Database.class located?
Avatar of MFos239

ASKER

The Database.class file is in this dir:

c:\web\WEB-INF\classes\com\backend\db\

and my source file that I am compiling is in:

c:\web\gui\testapplet.java

I used this line to compile the code:

javac -classpath c:\web\WEB-INF\classes c:\web\gui\testapplet.java

and it compiles without error, but when I try to run the applet in a browser I get java.lang.NoClassDefFoundError.
Were your source is located and how you compiled it has no impact on executing it.

Where is your codebase?
Avatar of MFos239

ASKER

Codebase? Not sure what you mean.
Codebase is the where your applet looks for classes on the server. If you haven't specified one in your applet html tag then it default to the location of the html file.
Where is your html file located on the server?
Additionaly, if your box is MS windows bases, the classpath used by browsers is not the one defined in either the CLASSPATH environment variable or the system environment setting; instead, its defined in the system registry under the specific browsers entries.
Yes but including it in the browsers classpath will only fix the problem on the local machine. If you make iut available at the codebase on the server then it will be available to ALL clients.
Avatar of MFos239

ASKER

Well...

I have this in my applet display code. Applets seem to work fine if my code refers solely to standard java library classes and its local (in same file) classes.

<object name="moduleApplet" classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" width=100% height=400 codebase="http://java.sun.com/products/plugin/1.2/jinstall-12-win32.cab#Version=1,2,0,0">
<param name=code value="testapplet.class">
<param name=codebase value="/gui/">
...
<comment><embed type="application/x-java-applet;version=1.2" java_CODE="testapplet.class" java_CODEBASE="/gui/" width=600 height=400 pluginspage="http://java.sun.com/products/plugin/1.2/plugin-install.html"><noembed></comment>
</noembed></embed></object>
Avatar of MFos239

ASKER

I should mention this is in a file c:\web\loadapplet.html
and that c:\web is the documentroot
Avatar of MFos239

ASKER

hello...  My name is brendon..  I'm the other developer for the project that Mfos239 is asking this question for.  I'm now going to give my own explanation of the problem.

We're working on a web site which uses jsp.  The container that we're using for this site is Resin if any of you are familiar with that.  We've written a bunch of different classes which we use in the jsp environment.  What we're now trying to do is create an applet which also makes use of these classes.  All of the classes are in the web site's WEB-INF/classes folder.  Now, a lot of these classes import one another and there is no problem there.  The thing is, however, we don't hand compile those files.  Resin takes care of that.  When we first attempted to compile the module classes (which are outside of the WEB-INF folder), it couldn't find the classes.  Well, the obvious mistake there is that we didn't put the path of those other classes in the CLASSPATH.  So now we have.  The modules now compile, however, the applet refuses to initialize.  So the question is....  What the hell is going on?????  :)  Any help greatly appreciated.  
Avatar of MFos239

ASKER

Making it worth 250
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MFos239

ASKER

I am now able to get my java applet to load if I put my classes in the same folder as my applet class file.  What I would like to do is put my classes in a separate folder.  The reason for this is that I have multiple applets that use my classes and each applet resides in its own folder.  I often update the classes and it would be nice to be able to have the classes in one location rather than having a copy of the classes in each applet folder.  That would be one big pain in the neck which it seems I should be able to avoid.  I don't want to have to copy the classes to each applet folder everytime i make a change to the classes.

Perhaps what I'm looking for is a way to specify a codebase with multiple directories.  Is there a way to do that?
i don't think you can specify multiple codebases.
Perhaps you could use symbolic links to point to your db classes directory from each applets codebase.