Hello there guys,

I've got a strange problem...

I'm using NetBeans IDE v5.5 and when I run my application inside netbeans by clicking 'Run main project' it runs and everything works correctly.

Now when I go to the dist folder and double click it... (the Jar file is assicated with a JRE) it half loads but none of the images are loaded. I can't understand why it works in the IDE but not outside?

I've refrerenced the images using the following code:

private static final String BRAODCASTING = "images/satelite.gif";
private  final URL IMAGE_BROADCAST_URL ;
...
...
 img = new ImageIcon (IMAGE_NOTBROADCAST_URL.getPath ());

Any ideas?

Recommended Answers

All 11 Replies

Your IDE is most likely treating it's project root directory as the root context when it executes. If you are clicking on a jar in the dist folder, that is most likely the root context. Try dropping a copy of your images directory into the dist folder. If it runs fine then you'll know that is the issue.

We actually include our images folder on the classpath and use

getClass().getResource(imagePath)).getImage()

to load an image.

the image folder is included in the project itself. This is also included in the jar file at runtime...

Perhaps i miss understood?

Try

new ImageIcon( getClass().getResource(IMAGE_NOTBROADCAST_URL.getPath ())).getImage()

. You may need to alter the classpath as well.

My bad, brackets problem... the Image returns a Image not an ImageIcon... I guess that raises the question as to whether I'm ment to be using ~Image or ImageIcon... Interesting...

the .getImage() isn't a functiuon thats assicated with this file type??

Sorry, mistype from hacking two different sources together

new ImageIcon(getClass().getResource(IMAGE_NOTBROADCAST_URL.getPath ())).getImage()

if "img" is an Image reference. If it's an ImageIcon, then you don't need the getImage() call;

My bad, brackets problem... the Image returns a Image not an ImageIcon... I guess that raises the question as to whether I'm ment to be using ~Image or ImageIcon... Interesting...

That just depends on what else you are doing with it :) If a method needs an Image reference you can just call getImage() if you have an existing ImageIcon object.

Hmm ive added the followin code

img = new ImageIcon( getClass().getResource(IMAGE_NOTBROADCAST_URL.getPath ()));

However this throws a null pointer exception... More due to the fact it can't find the image. I've extracted the contents of the jar file using winrar and I can confirm that its in the correct directory structure!

Just to give the solution to everyone (we worked it out over IRC).
I think the getClass() method didn't really do what it was supposed to be doing. Instead using ClassName.class.getResource() did the job.

Black Box

solved! Thanks guys :)

Just to give the solution to everyone (we worked it out over IRC).
I think the getClass() method didn't really do what it was supposed to be doing. Instead using ClassName.class.getResource() did the job.

Black Box

Glad you got it worked out there. getClass() works fine in our app context here, but as usual with varying application structures, you have to fiddle around a bit sometimes to get context issues resolved.

You can probably even ditch the URL variable and just use the original string path, since you're turning around and calling getPath on the URL in the getResource() call.

We ended up using the following:

new ImageIcon(ClassName.class.getResource("images/img.jpg"))

And it's true what you say, it all depends on context issues.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.