jar only: load pics works,  via webstart: err

strange java…

i used
*


myImage = javax.imageio.ImageIO.read( ClassLoader.getSystemResource( "myImage.xxx"));  

to load a pic within a jar, and it worked fine.

However, if the jar is started via webstart, it doesn’t work anymore!!!
I just get:


java.lang.IllegalArgumentException: input == null! 
 at javax.imageio.ImageIO.read(Unknown Source) 
...points to *

any idea? solution?

I think you want getClass().getResource(“myimage.xxx”) instead of ClassLoader.getSystemResource(“myimage.xxx”).

If I understand correctly, WebStart uses a different classloader than the system instance; so using getSystemResource() is going to cause your program to fail.

one of my webstart apps uses that for loading images in the jar to be put in the interface. Code generated by netbeans.


runningImage=new javax.swing.ImageIcon(this.getClass().getResource("images/running.png"));

you were both very very close, the correct solution seems to be:


getClass().getClassLoader().getResource("myImage.xxx");

…now, how to access it in a static context?! :stuck_out_tongue:

[quote]you were both very very close, the correct solution seems to be:


getClass().getClassLoader().getResource("myImage.xxx");

…now, how to access it in a static context?! :stuck_out_tongue:
[/quote]
Ahem getClass().getResource(“myImage.xxx”) is the same thing. It’s a convenience method to save you from typing “getClassLoader()”. :wink:

As I said, the core of your problem was that you were forcing the System classloader, when your data may have been loaded by some other classloader. In the case of Webstart, your code is probably loaded inside a URLClassLoader.

[quote]you were both very very close, the correct solution seems to be:


getClass().getClassLoader().getResource("myImage.xxx");

…now, how to access it in a static context?! :stuck_out_tongue:
[/quote]
awful, but…

new Integer(0).getClass().getClassLoader().getResource("myImage.xxx");

works. :slight_smile:
You might use anything that pleases you.
[edit] Oh, maybe it will show the original classloader… dunno… would be interested in knowing. Normally, it should not be system classloader, or imho it would be a security problem…

oops, i spoke way too quickly :-X


myImage = javax.imageio.ImageIO.read( * );

Here is a summary of what happen if i replace * with following code:


new File(...)

works, but not when it’s in a jar


this.getClass().getClassLoader().getResource(...)

works as jar but not webstart


this.getClass().getResource(...)

doesn’t work at all here.


ClassLoader.getSystemResource(...)

works as jar but not webstart

I guess the only thing left is to try it with ImageIcon but also here i’m curious to see if it will work.

Where is your image located inside the JAR file? If it’s in the base directory, you need to do something like this:


ImageIO.read(getClass().getResource("/myimage.png"));

Usually I keep all of my images inside an “images” folder. This allows them to be accessed via the following method (note the slashes):


ImageIO.read(getClass().getResource("/images/myimage.png"));

As for getClass().getResource() vs. getClass().getClassLoader().getResource(), this is what the JavaDocs say about the former:

This method delegates the call to its class loader, after making these changes to the resource name: if the resource name starts with “/”, it is unchanged; otherwise, the package name is prepended to the resource name after converting “.” to “/”.

yeah, right, it’s my fault. I thought of adding that slash but didn’t compiled correctly before running. It works, indeed.

…and now that i wanted to test the ImageIcon thing i get even more annoying problems, jws just won’t even download my jar!!! arrrrgghhh! it’s driving me crazy! i shut down my firewall, antivirus, restarted my PC… nothing changes it!
i can download the file via internet or any ftp program without problems, jws downloads other programs fine, but not mine!!! when i’m starting my jnlp, it starts downloading and stops it after 5 sec!!! what on hell is this!!!

Go ahead and email me the JAR and JNLP file. At the very least, I can try hosting them on my webserver to see if they’ll work.

Just a wild guess, but might the problem be that your server has the wrong MIME type for the JNLP file? i.e. Every time you click on the link, you get a text file instead of having Webstart launch?

oh, sorry for the delay, i was gone outside do get fresh air because i strated to become crazy. I appreaciate greatly your help proposal… but now it works again, and again, i don’t know why but it works! ???

the only thing that seems to work is this ugly line:


Image myImage = (new ImageIcon(this.getClass().getResource("/myImage.xxx"))).getImage();

…but that’s better than nothing. and the (new Integer(0))… trick doesn’t seem to work (again, it’s ok in the jar but doesn’t work via WS). I stopped to try understanding this crazyness, it’s too much! :stuck_out_tongue:

and again, thanks a lot for your great help!

In the interest of enabling webstart on my own game I looked up this site:
http://wiki.java.net/bin/view/Games/HowToUseWebstart

On the page I found this:

[quote]// Getting hold of a URL to sprite
Thread.currentThread().getContextClassLoader().getResource(“sprites/mySprite.png”);

// Getting hold of a stream to a sound
Thread.currentThread().getContextClassLoader().getResourceAsStream(“sounds/bang.wav”);
[/quote]
Is this new or helpful? I just found it and saw it wasn’t listed as one of the attempts here. Is it equivalent to one of the others? Hope maybe it helps!

First make a class called ResourceAnchor and put this code into it:

public class ResourceAnchor
{
      public ResourceAnchor()
      {
            
      }
}

Then make a ClassLoader object one time in your code from the resourceanchor like this:

      ClassLoader cLoader = ResourceAnchor.class.getClassLoader();

When you need to load an image to an ImageIcon or however you wanna do it, do it like this:

      ImageIcon ii = new ImageIcon(cLoader.getResource("images/pic1.jpg"));

Btw make sure all your cases of filenames match what your trying to load as webstart is finicky with those.