Solved

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

Posted on 2001-09-07
18
418 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
recursion example 16 111
JavaScript/Java - Changing an image background color 4 56
Java SE 8u111  Lot of stuff broke 11 54
Arrays.asList  VS  ArrayList 4 45
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

896 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

14 Experts available now in Live!

Get 1:1 Help Now