• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 445
  • Last Modified:

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.
0
MFos239
Asked:
MFos239
1 Solution
 
rrzCommented:
What does your Java Console print out?
0
 
MFos239Author Commented:
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();
0
 
objectsCommented:
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.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
MFos239Author Commented:
Database is in the classpath
0
 
objectsCommented:
Sorry what classpath are u referring to?
Where is the file Database.class located?
0
 
MFos239Author Commented:
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.
0
 
objectsCommented:
Were your source is located and how you compiled it has no impact on executing it.

Where is your codebase?
0
 
MFos239Author Commented:
Codebase? Not sure what you mean.
0
 
objectsCommented:
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?
0
 
jpk041897Commented:
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.
0
 
objectsCommented:
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.
0
 
MFos239Author Commented:
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>
0
 
MFos239Author Commented:
I should mention this is in a file c:\web\loadapplet.html
and that c:\web is the documentroot
0
 
MFos239Author Commented:
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.  
0
 
MFos239Author Commented:
Making it worth 250
0
 
objectsCommented:
Whether its jsp or whatever is irelevant, the applet executes (on the client) independant of these technologies.

The applet looks in the codebase on the server for any classes that it needs as well as its local classpath (which may not be the same as the CLASSPATH environment variable).
To make your applet work you need to make all classes necessary available at the codebase (or wrap them all in a jar and use the archive applet parameter tag).

eg. Database.class needs to be put in:

c:\web\gui\com\backend\db\

0
 
MFos239Author Commented:
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?
0
 
objectsCommented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now