Loading resourcen in WebStart

Hi,
I’ve posted a game that I’m working here. I did the JNLP file for it but when I run it with webstart causes an exception. If I download the jar file it runs ok. (Also is running OK in eclipse)
I’m using the Kev’s tip to load resources with

Thread.currentThread().getContextClassLoader().getResourceAsStream(file)

I don’t know if it is a problem with the data or the method used to access the images.

Here are the links:
Executable Jar
WebStart

Rafael.-

The method is right, but the string that you should be supplying is from the root directory. So say you have this:


com
    res
        image.png
    stuff
        myClass

You would access image.png from myClass by using “com/res/image.png”. Is that what you are doing?

DP

“/com/res/image.png”

The leading slash is important.

My directories are


App.class
object
  Ball.class
  Board.class
  *
sound
  SoundFXController.class
  *
res
  balls.png
  stars.png
  fx
    plick.wav
    *

And my code is the next

image = ImageIO.read(Thread.currentThread().getContextClassLoader().getResourceAsStream("res/combo.png"));

Before I was using

image = ImageIO.read(getClass().getClassLoader().getResourceAsStream("/res/combo.png"));

When I changed the method the leading slash didn’t work, so I removed it.

I think the access is working because the sound fx are loaded first. The problem is that the images don’t work.

This happens only in the webstart, but not if executing the jar. (The webstar uses the same jar).

Rafael.-

I don’t think the leading slash is a good idea. On Linux/UNIX, the leading slash indicates the very top of your directory structure … You want to access your files using a relative path, so DO NOT use a leading slash.

This is a java thing. NOT a os thing.

The leading slash makes its pseudo absolute (relative to the roots of the classpath).

See this example:
http://kaioa.com/k/jarimage.jar (doubleclickable/src included)

See javadoc:
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html#getResourceAsStream(java.lang.String)

The leading slash thing is only true if you’re using


Class.getResourceAsStream()

because in that case the path specified has the context of the class. You need the leading slash to get out of the Class’s package context. If you’re using


ClassLoader.getResourceAsStream() 

as with getContextClassLoader() then the leading “/” is redundant because there is not a package context from a class. Unfortunately using the leading slash in this method fails on some systems.

Kev

Well,
Then the access is OK, but the image loading fails.

The jar executed directly works. But webstart gives me an error trying
to load the image.

Has webstart some kind of restriction in the resources loaded inside a jar???

Could you try the links to see if it works??

WebStart
Executable Jar

  Rafael.-

Webstart works fine here (Windows). What platform is it failing on? Are you wrapping the input streams from the class loader in a BufferedInputStreams - that needed to be done at one point - not sure if it’s still true.

Kev

Compiled in jdk1.4.2_03 in Win2000
Tested in the same PC.

I didn’t try it in my home… (jdk1.5.0 in WinXP)

Wrapping in a BufferedInputStream… I’ll try that also.

Rafael.-

EDIT: The BufferedInputStream workaround fixed it.

Thanks.