Re: Applet ClassLoaders and resources

Andrew Thompson wrote:
On Jan 7, 1:57 pm, Wayne <nos...@xxxxxxxxxxxxxxx> wrote:
It took some detective work but I learned the one who
gave that advice, "dietz333", was really Andy Herrick,
the top man of Java SE deployment at Sun. A bit more
work and I found a working email address. I've asked
him to clarify. If I hear back I'll post his answer
(so Roedy can update the Java Glossary).

Good move. You are not the first to wonder
about that advice, and will surely not be the

A brief exchange between Wayne and Andy Herrick (Java Engineering):

Wayne wrote:
Subject: Java Question - What's wrong with getClass().getResource()?

Sorry to bother you on facebook but I couldn't find another email
address for you. You posted on several Sun forums (as dietz333; it
was hard to track you down!) that this method was not safe, and that
one should always use
But you never said why. The question has just come up again on
netnews, "Applet ClassLoaders and
Resources". It was questioned why used the Thread method and the
answer was "dietz333" said the other was unsafe.

Thanks for your time! If you feel inclined you can post an answer
directly to c.l.j.p, or send me a URL of a posting with an answer and
I'll post it for you.

Andy replied:
I'm not clear exactly what you are asking, or which post you are
referring to. I've made many over the years, some of which still apply
to current versions of Java, Java Web Start, and Java Plug-in, some that
may not. (In other cases I have been just plain wrong).

In general, to load resources that are contained in the jars included in
a jnlp file of a jnlp application, or archive tags of an plug-in applet,
you have to use the JNLPClassLoader, PluginClassLoader, or
Plugin2ClassLoader that has the knowledge of those jars.

The question then is how to find that class loader.

Normally, either Thread.currentThread().getContextClassLoader(), or
this.getClass().getClassLoader() will work fine, however there were some
instances where this.getClass().getClassLoader() could cause a problem,
so I normally recomend Thread.currentThread().getContextClassLoader().

Is this what you are asking ? When could
this.getClass().getClassLoader() not return the JNLPClassLoader in a
Java Web Start application ?

Wayne replied:
That's exactly right; when would getClass().getResource()
fail at the same time Thread.currentThread().getContextClassLoader().getResource()

Andy replied:
I'll have to look back and find the instances. It had to do
with either code called from native code thru jni, or code
called from certain event handlers. I do remember the system
class loader was being returned in some cases.

Most likely this was due to a bug in Java 1.4.2 or 1.5.0
that no longer exists in JDK6 train.



So the upshot is, there is nothing wrong with either method!