Solved

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

Posted on 2001-09-07
18
416 Views
Last Modified: 2013-11-23
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
Comment
Question by:MFos239
18 Comments
 
LVL 27

Expert Comment

by:rrz
ID: 6466894
What does your Java Console print out?
0
 

Author Comment

by:MFos239
ID: 6467253
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
 
LVL 92

Expert Comment

by:objects
ID: 6467836
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
 

Author Comment

by:MFos239
ID: 6468181
Database is in the classpath
0
 
LVL 92

Expert Comment

by:objects
ID: 6468185
Sorry what classpath are u referring to?
Where is the file Database.class located?
0
 

Author Comment

by:MFos239
ID: 6468787
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
 
LVL 92

Expert Comment

by:objects
ID: 6469175
Were your source is located and how you compiled it has no impact on executing it.

Where is your codebase?
0
 

Author Comment

by:MFos239
ID: 6469344
Codebase? Not sure what you mean.
0
 
LVL 92

Expert Comment

by:objects
ID: 6469419
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 6

Expert Comment

by:jpk041897
ID: 6478459
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
 
LVL 92

Expert Comment

by:objects
ID: 6478506
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
 

Author Comment

by:MFos239
ID: 6478685
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
 

Author Comment

by:MFos239
ID: 6478697
I should mention this is in a file c:\web\loadapplet.html
and that c:\web is the documentroot
0
 

Author Comment

by:MFos239
ID: 6478713
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
 

Author Comment

by:MFos239
ID: 6478715
Making it worth 250
0
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 6478735
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
 

Author Comment

by:MFos239
ID: 6501618
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
 
LVL 92

Expert Comment

by:objects
ID: 6502819
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

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now