LWJGL Forum

Archive => Resolved Bugs/RFE => Topic started by: arielsan on February 16, 2011, 17:39:15

Title: [FIXED] Applet Loader - Thread context class loader doesn't return resources ....
Post by: arielsan on February 16, 2011, 17:39:15
Me again me....

When using Thread.currentThread().getContextClassLoader() in an Applet loaded by the LWJGL Applet Loader to search for resources, it fails, it never finds resources inside the classpath (inside some of the jars) because the context class loader will never search in the jars loaded by the Applet Loader.

I have made a project to replicate this problem (only replicating some code of the Applet Loader), and made a bug fix too by setting the context class loader to the URLClassLoader created.

Dunno if there is a constraint for setting the Thread context class loader.

Attach 1: the sample project to see this behavior, it is a maven project but it has no dependencies.
Attach 2: a patch for this bug fix.
Title: Re: [BUG] Applet Loader - Thread context class loader doesn't return resources ....
Post by: arielsan on February 16, 2011, 18:02:03
The example project has an Applet which tries to load another Applet from an external jar in a similar way LWJLG Applet Loader does.

In src/main/resources we the jar to be loaded with the other Applet inside. This Applet tries to load an image.png from the same jar (should be able to do that because the image is in its own classpath).

When context class loader is not set, the new Applet fails when trying to get the image.png using Thread.currentThread().getContextClassLoader(). Else if context class loader is set, the Applet loads the image.

I wanted to explain a bit the project because I didn't in the previous post.

P.S. : attached again the project with a fix so it can be built using maven.
Title: Re: [BUG] Applet Loader - Thread context class loader doesn't return resources ....
Post by: kappa on February 16, 2011, 19:25:53
oh nice, looks like a great catch.

I'm guessing this fixes the Guice issue you had a while back too.
Title: Re: [BUG] Applet Loader - Thread context class loader doesn't return resources ....
Post by: arielsan on February 16, 2011, 21:00:01
I tested using only one thread but it should be set in the same thread running the switchApplet, else it wont work.

I'll test it in the Applet Loader and if it works, I'll copy the new patch.
Title: Re: [BUG] Applet Loader - Thread context class loader doesn't return resources ....
Post by: arielsan on February 16, 2011, 21:50:06
Confirmed and tested, I had to add it also to the same thread of the switchApplet() call this:

Code: [Select]
// make applet switch on EDT as an AWT/Swing permission dialog could be called
EventQueue.invokeAndWait(new Runnable() {
            public void run() {
try {
// FIX2: should be set for same thread of the applet also.
Thread.currentThread().setContextClassLoader(classLoader);

switchApplet();
} catch (Exception e) {
fatalErrorOccured("This occurred while '" + getDescriptionForState() + "'", e);
}
setState(STATE_DONE);
repaint();
            }
});

Here (http://www.gemserk.com/floatingislands-jnlp.html) is a working example (warning link could be removed or could be broken tomorrow). This game was doing resources loading by using the thread context class loader, and now it works (without any game code modification).

Title: Re: [BUG] Applet Loader - Thread context class loader doesn't return resources ....
Post by: kappa on February 17, 2011, 19:12:46
thx for the fix, good job.

Fix applied to svn.