[CLOSED] AppletLoader getImage() is not getting image from classpath

Started by arielsan, January 28, 2011, 15:51:17

Previous topic - Next topic

arielsan

AppletLoader is not getting right the images (appletlogo.png, etc) from class path in my case, taking a look at the code:

URL url = Thread.currentThread().getContextClassLoader().getResource("/"+s);

	// if image not found in jar, look outside it
	if (url == null) {
		url = new URL(getCodeBase(), s);
	}


first line is always returning null in my case (and I suppose it is for everyone the same).

After some chat on irc with kappaOne and MatthiasM, I believe there is a bug with adding the prefix "/" when getting a resource using the ClassLoader, this differs from using the Class.getResource(), that one accepts rooted resources but ClassLoader always starts looking at the root. There is some info in the Class.getResource() docs.

I believe there are several solutions, one could be to use AppletLoader.class.getResource("/" + s) instead class loader, another one is to use classLoader.getResource(s), without the leading "/".

What do you say?

Could be some difference between java < 6 and java >= 6 or something like that?

kappa


arielsan

I know I asked to close this request but it keeps failing to get the image from the classpath (from the applet loader jar) to me in some cases...

Right now, whenever I test the Applet Loader, I have to copy the images to the place where the codebase is pointing at, for example, where the game is, so the applet loader is downloading 2 times each image (once when the applet loader jar is downloaded, and once when they are loaded using getImage()).

what do you say about some thing like this:

URL url = Thread.currentThread().getContextClassLoader().getResource("/"+s);

			// check without the "/" 
			if (url == null) 
				AppletLoader.class.getClassLaoder().getResource(s);

			// if image not found in jar, look outside it
			if (url == null) {
				url = new URL(getCodeBase(), s);
			}


I don't like the solution too much, but it a way to maintain the bug fix you said and also look in the AppletLoader class path (where the images are).

kappa


arielsan

URL url = url = new URL(getCodeBase(), s);
				
			// if image failed to load, try another method
			if (url == null) {
				Thread.currentThread().getContextClassLoader().getResource(s);
			}

			Image image = super.getImage(url);

			// wait for image to load
			MediaTracker tracker = new MediaTracker(this);
	        tracker.addImage(image, 0);
	        tracker.waitForAll();

	        // if no errors return image
	        if (!tracker.isErrorAny()) {
	        	return image;
	        }


You forgot url = Thread.....

if (url == null) {
				url = Thread.currentThread().getContextClassLoader().getResource(s);
			}

kappa


arielsan

Sorry to keep bothering... but, it is still wrong:

URL url = url = new URL(getCodeBase(), s);
				
			// if image failed to load, try another method
			if (url == null) {
				url = Thread.currentThread().getContextClassLoader().getResource(s);
			}


That url will never be null... we must check after trying to load the image, I've attached a patch to make this, tested using both ways (from junit and from a deployed applet), of course, could still have bugs.

Also, url was assigned twice (url = url = ....).

Don't hate me

kappa

meh, remind me to not code straight after waking up, sorry :)

will fix tonight.

kappa