views:

106

answers:

1

Hi I need to get a resource image file in a java project. What I'm doing is:

URL url = TestGameTable.class.getClass().
          getClassLoader().getResource("unibo.lsb.res/dice.jpg");

The directory structure is the following:

unibo/
  lsb/
    res/
      dice.jpg
    test/
    ..../ /* other packages */

The fact is that I always get as the file doesn't exist. I tryid many different paths, but I couldn't solve the issue. Any hint?

+4  A: 
TestGameTable.class.getResource("/unibo/lsb/res/dice.jpg");
  • leading slash to denote the root of the classpath
  • slashes instead of dots in the path
  • you can call getResource() directly on the class.
Bozho
To the point! (15ch)
BalusC
Just be aware that `Class#getResource` and `ClassLoader#getResource` are using different strategies to map the name to a location. LucaB's example actually uses the ClassLoader from Class<java.lang.Class> (`SomeClass.class.getClass()`), but that's probably a mistake and not on purpose.
jarnbjo
@jambjo yes, I assumed it's a mistake.
Bozho
It is a mistake. Thanks for the hint
LucaB