Hello Guest

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

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

Code: [Select]
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?

*

Offline kappa

  • *****
  • 1313
Re: [BUG] AppletLoader getImage() is not getting image from classpath
« Reply #1 on: January 28, 2011, 16:40:54 »
Not a bug, closed.

Re: [CLOSED] AppletLoader getImage() is not getting image from classpath
« Reply #2 on: February 02, 2011, 14:01:48 »
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:

Code: [Select]
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).

*

Offline kappa

  • *****
  • 1313
Re: [CLOSED] AppletLoader getImage() is not getting image from classpath
« Reply #3 on: February 02, 2011, 22:36:09 »
k, attempted a fix, hopefully should work for you now.

Re: [CLOSED] AppletLoader getImage() is not getting image from classpath
« Reply #4 on: February 03, 2011, 00:54:09 »
Code: [Select]
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.....

Code: [Select]
if (url == null) {
url = Thread.currentThread().getContextClassLoader().getResource(s);
}

*

Offline kappa

  • *****
  • 1313
Re: [CLOSED] AppletLoader getImage() is not getting image from classpath
« Reply #5 on: February 03, 2011, 09:36:12 »
You forgot url = Thread.....

Doh! thx for spotting that. Fixed.

Re: [CLOSED] AppletLoader getImage() is not getting image from classpath
« Reply #6 on: February 03, 2011, 15:15:36 »
Sorry to keep bothering... but, it is still wrong:

Code: [Select]
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

*

Offline kappa

  • *****
  • 1313
Re: [CLOSED] AppletLoader getImage() is not getting image from classpath
« Reply #7 on: February 03, 2011, 15:51:39 »
meh, remind me to not code straight after waking up, sorry :)

will fix tonight.

*

Offline kappa

  • *****
  • 1313
Re: [CLOSED] AppletLoader getImage() is not getting image from classpath
« Reply #8 on: February 03, 2011, 22:18:30 »
ok, done, hopefully should be correct this time.