Applet class loading enquiry

Q

Qu0ll

If an applet comprises say 40 classes in 4 separate JARs, is Java "smart"
enough to only download the individual class files from within each JAR as
they are first accessed or will accessing one class in a JAR cause a
download of the entire JAR before methods in that class can be invoked?

--
And loving it,

-Q
_________________________________________________
(e-mail address removed)
(Replace the "SixFour" with numbers to email me)
 
R

Roedy Green

If an applet comprises say 40 classes in 4 separate JARs, is Java "smart"
enough to only download the individual class files from within each JAR as
they are first accessed or will accessing one class in a JAR cause a
download of the entire JAR before methods in that class can be invoked?

The server always sends a complete jar at a time. It is just a file as
far as the server is concerned.
 
K

Knute Johnson

Qu0ll said:
If an applet comprises say 40 classes in 4 separate JARs, is Java
"smart" enough to only download the individual class files from within
each JAR as they are first accessed or will accessing one class in a JAR
cause a download of the entire JAR before methods in that class can be
invoked?

The latter I believe.
 
Q

Qu0ll

Roedy Green said:
The server always sends a complete jar at a time. It is just a file as
far as the server is concerned.


OK, thanks. But if the applet comprises 4 JARs, do the whole 4 files get
downloaded immediately the applet loads or are they only loaded as classes
within them are accessed by the applet?

--
And loving it,

-Q
_________________________________________________
(e-mail address removed)
(Replace the "SixFour" with numbers to email me)
 
T

Tom McGlynn

OK, thanks. But if the applet comprises 4 JARs, do the whole 4 files get
downloaded immediately the applet loads or are they only loaded as classes
within them are accessed by the applet?

It would certainly be interest to test this out, but my guess would be
that jars will be loaded in the order they appear in the class path
until all classes needed are found. So even if you load just a single
class, but it's found in the last jar in the path all four jars will
need to be downloaded. However if everything can be resolved without
referencing trailing jars, then I wouldn't expect them to be
downloaded.
If you find out let us know...
Regards,
Tom McGlynn
 
Q

Qu0ll

It would certainly be interest to test this out, but my guess would be
that jars will be loaded in the order they appear in the class path
until all classes needed are found. So even if you load just a single
class, but it's found in the last jar in the path all four jars will
need to be downloaded. However if everything can be resolved without
referencing trailing jars, then I wouldn't expect them to be
downloaded.
If you find out let us know...

That's a nice theory and I hope it does work that way but it looks to me as
though all JARs are loaded at the same time. I cannot be sure as I haven't
conducted a definitive test but from the time it takes for my applet to load
I'd say that it's all at once.

Any suggestions for a way to test to find out for sure?

--
And loving it,

-Q
_________________________________________________
(e-mail address removed)
(Replace the "SixFour" with numbers to email me)
 
L

Lew

Qu0ll said:
That's a nice theory and I hope it does work that way but it looks to me
as though all JARs are loaded at the same time. I cannot be sure as I
haven't conducted a definitive test but from the time it takes for my
applet to load I'd say that it's all at once.

Even if JARs are loaded on demand, the dependency structure could cause all
JARs to come in at the beginning anyway. For example, a descendant class in
JAR One that inherits from a base class in JAR Two will force JAR Two to come
in. If the base class references a utility method from a class in JAR Three,
here comes the third JAR. Meanwhile back in JAR One a static variable is
initialized from a variable provided by a static method in a class from JAR
Four. Et voilà.
 
Q

Qu0ll

Lew said:
Even if JARs are loaded on demand, the dependency structure could cause
all JARs to come in at the beginning anyway. For example, a descendant
class in JAR One that inherits from a base class in JAR Two will force JAR
Two to come in. If the base class references a utility method from a
class in JAR Three, here comes the third JAR. Meanwhile back in JAR One a
static variable is initialized from a variable provided by a static method
in a class from JAR Four. Et voilà.

That's all true but what I am actually doing is loading classes in all but
the actual applet JAR using Class.forName() so there are no dependencies
between the JARs.

--
And loving it,

-Q
_________________________________________________
(e-mail address removed)
(Replace the "SixFour" with numbers to email me)
 
A

Andrew Thompson

If anappletcomprises say 40 classes in 4 separate JARs, is Java "smart"
enough to only download the individual class files from within each JAR as
they are first accessed or will accessing one class in a JAR cause a
download of the entire JAR before methods in that class can be invoked?

That depends. If the Jar's are listed in the
archives attribute of the applet element, no.

<http://java.sun.com/j2se/1.4.2/docs/guide/misc/applet.html>
"ARCHIVE = archiveList
This OPTIONAL attribute describes one or more
archives containing classes and other resources
that will be "preloaded". .."

OTOH, it is not necessary to list archives in the
archive attribute. An applet can get classes from
oher Jar's as well, using (for e.g.) an URLClassLoader.

Java webstart has more options for supplying Jar's
lazily, but this use case would require some extra
programming. Calls to instantiate a class (or use
a static method etc.) from a 'lazy' jar will generally
invoke the download and caching of the archive, but
that does not work for reflection and Class.forName(),
which will fail unless the class has already been
cached locally.
 
R

Roedy Green

OK, thanks. But if the applet comprises 4 JARs, do the whole 4 files get
downloaded immediately the applet loads or are they only loaded as classes
within them are accessed by the applet?

If you use Java Web Start to load your Applet, you get to specify
eager or lazy loading in your jnlp file.. I suppose it is up to the
browser which style of load to use for vanilla Applets which to use.

You can do an experiment. Write an applet two jars in the archive
line but that ignores the second jar. Watch with a sniffer to see what
loads.
See http://mindprod.com/jgloss/sniffer.html
..
 
L

Lew

Qu0ll said:
That's all true but what I am actually doing is loading classes in all
but the actual applet JAR using Class.forName() so there are no
dependencies between the JARs.

In what way would that eliminate dependencies on other JARs?

The only way that it would is if the class you're instantiating doesn't
inherit from or use classes in other JARs.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top