[FIXED] Applet Loader - Thread context class loader doesn't return resources ....

Started by arielsan, February 16, 2011, 17:39:15

Previous topic - Next topic

arielsan

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.

arielsan

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.

kappa

oh nice, looks like a great catch.

I'm guessing this fixes the Guice issue you had a while back too.

arielsan

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.

arielsan

Confirmed and tested, I had to add it also to the same thread of the switchApplet() call this:

// 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 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).


kappa