Solved

How applet loads classes?

Posted on 1998-05-07
5
324 Views
Last Modified: 2013-12-29
There is an applet which is sopposed to load all it's class and image files from a JAR archive.  The HTML file contains an ARCHIVE tag which references the archive on the Web server. This applet is run in the browser on the client machine. It is possible that the client machine where applet runs has same or similarly named classes to those used by applet stored locally and available through the classpath.  My questions are:

1. If the applet has a choice of loading the class file from JAR file on the server or locally from a CLASSPATH, which one will it choose? What if the server and a client is the same machine where classes can be loaded both from JAR file and from CLASSPATH. Is there a documentation which covers this topic?

2. The applet uses GIF images which may also be available both in the JAR file and on the local machine (the directory structure in the JAR file and on local machine regarding .class and .gif files is the same).  It looks like the applet will try to load the class file from the same source it loaded class files from. Is that true?

3. If the applet tries to load image file from a local directory but fails due to security restrictions, should it try to load it from an archive?
0
Comment
Question by:msmolyak
  • 3
  • 2
5 Comments
 
LVL 6

Accepted Solution

by:
jpk041897 earned 150 total points
ID: 1220728
>1. If the applet has a choice of loading the class file from JAR file on the server or locally
>from a CLASSPATH, which one will it choose? What if the server and a client is the
>same machine where classes can be loaded both from JAR file and from CLASSPATH.
>Is there a documentation which covers this topic?

The Applet will look at the cache first (controlled by the browser, not Java), then the CLASSPATH and finaly the JAR file. As a result of this, behaviour for a situation as the one that you mention will vary. The first time the Applet is loaded, the class will be read from either the CLASSPATH (if the class exists in it) or will download the JAR file, store it in the cache and load it from there. After the first load though, the class will be in the cache (untill its cleared) and will be loaded invariably from there.

>    2. The applet uses GIF images which may also be available both in the JAR file and >on the local machine (the directory structure in the JAR file and on local machine >regarding .class and .gif files is the same).  It looks like the applet will try to load the >class file from the same source it loaded class files from. Is that true?

Yes and no. Actualy it will be read from the cache as explained above.

>    3. If the applet tries to load image file from a local directory but fails due to security >restrictions, should it try to load it from an archive?

Certanly, if thats the behaviour yu desire. You can write your code to read from the cache/server while proccesing a security exception. Instead of letting the applet die, you can set a try/catch block that tests for security exceptions and attemts to load the immage from the JAR file instead.
0
 
LVL 5

Author Comment

by:msmolyak
ID: 1220729
Thank you for the answer. Could you possibly indicate the source for the information (that is the description of the way classes are loaded). I don't suppose it is in the language spec.

The problem I have with image files is the following.  When the applet loads it tries to load images from the CLASSPATH (does it mean that the classes also came from CLASSPATH), it then displays an error on the Java Console (possibly it throws an exception which gets caught) and proceeds loading from the JAR. So that part works reasonably well.  When the applet start running however, it only tries loading additional images from the CLASSPATH and upon getting a security exception does not try to load from the JAR.

You mentioned in your answer that I can tell it to load from JAR. How do you do it explicitly in the program? Is it possible to prohibit the program from reading from CLASSPATH or this is controlled by the browser?

Also, if the class or image file was not loaded due to security restrictions, it will not reside in the cahce, will it?
0
 
LVL 6

Expert Comment

by:jpk041897
ID: 1220730
>Could you possibly indicate the source for the information (that is the description of the way classes are loaded). I don't suppose it is in the language spec.

No its not in the language specs. Its in the security Sandbox specs for the diffrent browsers and the specs for the OS.

>The problem I have with image files is the following.  When the applet loads it tries to load images from the CLASSPATH (does it mean that the classes also came from CLASSPATH),

This is controlled by the browser and OS, when looking for a file, the browser (and applet) will look in the cache first (to make access faster) if not there it will look in the local directory (from which the browses .EXE file was run and which shoul not contain any classes anyways) then the CLASSPATH. (It should be noted that most browsers do not use the CLASSPATH enviromnet variable but a System registry CLASSPATH instead).


> it then displays an error on the Java Console (possibly it throws an exception which gets caught) and proceeds loading from the JAR. So that part works reasonably well.  When the applet start runninghowever, it only tries loading additional images from the CLASSPATH and upon getting a security exception does not try to load from the JAR.

This seems to indicate that you are attempting to load the immages from a server diffrent then the one you loaded the APplet from.

>You mentioned in your answer that I can tell it to load from JAR. How do you do it explicitly in the  program? Is it possible to prohibit the program from reading from CLASSPATH or this is controlled by the browser?

Default behaviour is controlled by the Browser. The eisiest way to control the loading process is to download a class library that will do it for you. Take a look at the free Osiris foundation class at: http://www.cyberlinklabs.com/ .

>  Also, if the class or image file was not loaded due to security restrictions, it will not reside in the cahce, will it?

No it wont.
0
 
LVL 5

Author Comment

by:msmolyak
ID: 1220731
I am still not very clear on how much control I have. You mentioned in your reply:

  You can write your code to read from the cache/server while proccesing a security   exception. Instead of letting the applet die, you can set a try/catch block that tests for   security exceptions and attemts to load the immage from the JAR file instead.

Would I have to write any special code to explicitly load from the JAR file. Now my code simply states the file name and it is up to the browser to find it. How to load from JAR file explicitly?

Also, restating the question asked above: during loading the applet fails to load GIF file from a local disk but then it loads it from the JAR file. When the applet starts running, it only try to load the image from the local drive but does not go to the JAR file. Can you explain that behavior?

Thank you again for your help.

Michael
0
 
LVL 6

Expert Comment

by:jpk041897
ID: 1220732
Would I have to write any special code to explicitly load from the JAR file. Now my code simply states the file name and it is up to the browser to find it. How to load from JAR file explicitly?

Two options:

1.- Use the name of the JAR file in the APPLET tag which causes the search order to be: cache, server, classpath.

2.- You could write explicit code to do it while procesing exeptions or use code like that provided by ciberlinklabs.com.


      Also, restating the question asked above: during loading the applet fails to load GIF file from a local disk but then it loads it from the JAR file. When the applet starts running, it only try to load the image from the local drive but does not go to the JAR file. Can you explain that behavior?

JAR files default behaviour is only designed arroud class files, i.e.: it only looks for .class extentions in the JAR file, not for .gif or .jpg files. Thats another reason the class lib I refered you to is useful.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

757 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

16 Experts available now in Live!

Get 1:1 Help Now