Weird getResource behaviour

Hello!

So far I haven't encountered any problems when doing the tutorials, they are a great way to get to know the enviroment, and I appreciate all the hard work put down to making jME what it is.



However, I have encountered a strange problem that is probably due to me not being very familiar with neither eclipse nor jME.



In the HelloStates tutorial I got to use getResource to load the image "Monkey.jpg". This worked. But when I tried to load one of my own pictures (256x256) that I put in the same folder… it wouldn't load. I got



VARNING: Could not load image…  URL was null. defaultTexture used.



I tried to load the monkey picture again and it worked. I tried to load "logo.jpg" which was also included in the same folder as "Monkey.jpg". It worked! I then copied the logo picture and renamed it "logo2.jpg" and tried to load it with no success. Same error message as before.



So my humble conclusion is that eclipse (which is the package I'm using) has somehow already been exposed to "Monkey.jpg" and "logo.jpg" and remembers them from before. When I ask it to load new pictures it just won't look in the target folder, because it's convinced that it already knows the contents. I'll try to figure out how the class works and post back if I find out the error.



I have no idea if I'm on the right track here, I tried searching the forum for the same error message and I came over a lot of material, but I just haven't been able to figure it out yet. Some suggested to refresh eclipse, I think I've done that; I used the command from the "File" menu.



So any help is highly appreciated!

Thx for reading.

When you want to load a resource, using the classloader, this resource has to be in the classpath. How do you load it?



URL resource = getClass().getClassLoader().getResource("jmetest/data/images/Monkey.jpg");



This resource is in the jar file, which is in the classpath. If you want to load your own resources, you have to make sure, that the location of that resource is in the classpath.

Try browsing to that folder in the eclipse package explorer, and refreshing it.

Thanks for your replies!

Alric said:

Try browsing to that folder in the eclipse package explorer, and refreshing it.


In package explorer, I right-clicked on the package with my jar-file and selected refresh to no avail. Was that what you meant that I should do?

Vardamir said:

When you want to load a resource, using the classloader, this resource has to be in the classpath. How do you load it?


URL resource = getClass().getClassLoader().getResource("jmetest/data/images/Monkey.jpg");



This resource is in the jar file, which is in the classpath. If you want to load your own resources, you have to make sure, that the location of that resource is in the classpath.


I read about classpaths on wikipedia, but I don't know if I fully understand it. So even if the picture I'm trying to use is in the same folder as the picture that works, it doesn't mean that it has to be in the same classpath? I don't get it :/

Thanks for helping!

I solved it!



The reason it didn't work was that I had to add the folder with the images in it to the classpath. I had no idea this was done "outside" the code. I don't know if I did this the right way, but I right-clicked my class, and chose Run as -> Open run dialog. There I found the tab "classpath" and clicked on "user entries". The "Advanced" button to the right got activated and in there I just chose "add external folder".



At first it still didn't work, but then I just removed the whole search path and just put the imagename.jpg in the string which defined which picture to take.


monkeyLoc=getClass().getClassLoader().getResource("pic.jpg");



This is probably old news to most of you reading this. But for that one dude that wants to get his or hers hands dirty with jME but have never coded in java or used eclipse before this might be helpful.

I still don't understand why the other pictures worked when none of the new ones did. They were in the same folder!