2

I have a Java project and have to load resources such as sounds or images, which worked pretty well until I exported it into a jar file, where the app crashed because I it couldn't access the resources. I found after some research that I should use getClass().getClassLoader().getResource() or Class.getResource(). But after trying all the possibilities with the first or second function, with or without the leading /, but each time, I get null as a result, and used res/images/bg.jpg as argument. My project hierarchy looks like this:

|src
 |Main.java
|res
   |images
      |bg.jpg

None of the solutions I've found on Stack Overflow or on Google worked for me. What am I doing wrong and what should I use?

EDIT

When exporting as jar, I am just right clicking on my project on Eclipse (without using any plug-ins), export and select runnable jar and explicitly declare my class Main as Classpath. When checking the content of the jar file, I can see the resources in the correct places.

user26830
  • 1,059
  • 4
  • 16
  • 25
  • Try something from http://stackoverflow.com/questions/2308188/getresourceasstream-vs-fileinputstream/2308388#2308388 . They should allow you to read from the jar. Also, do put your resources inside src. – Laurentiu L. May 12 '15 at 15:46
  • @LaurentiuL. I did try putting my resources inside src but doesn't solve anything – user26830 May 12 '15 at 15:51
  • I would suggest adding more detail here. You're not doing anything obviously wrong, but in classloading issues you have to be aware of what you're running, what your classpath is, how things were built, how they're being integrated and so on. – Trevor Brown May 12 '15 at 15:52
  • how about `/images/bg.jpg` – ZhongYu May 12 '15 at 15:54
  • @TrevorBrown Added some more informations, hope it helps. – user26830 May 12 '15 at 15:58
  • @user26830 how do you run the code? Command line or through eclipse? See my comment on one of the other answers if it's the latter. If it's the former, what is the command you are running and from where do you run it in relationship to your jar? – Trevor Brown May 12 '15 at 16:06
  • @TrevorBrown I tried both double-clicking to the jar file from the file explorer and running the `java -jar my-game.jar` from the terminal on the jar's folder. – user26830 May 12 '15 at 16:12

2 Answers2

0

The res folder should be a child of your src folder

|src
  |Main.java
  |res
    |images
       |bg.jpg

although standard practice would be to have the layout closer to

|src
  | main
    |java
      |package
        |Main.java
    |resources
      |images
        |bp.jpg
  • This does't change anything, I still get null. – user26830 May 12 '15 at 15:46
  • This answer assumes behaviors in the build system, and the OP didn't mention which build system is in use. – Trevor Brown May 12 '15 at 15:47
  • The edit with a src/main/java and src/main/resources layout now assumes that the OP is using maven. If the OP is using ant, then that layout is definitely not standard practice. – Trevor Brown May 12 '15 at 15:51
  • @TrevorBrown I am just using Eclipse without any plug-in. – user26830 May 12 '15 at 15:54
  • Could be the source of your problem then if you're using an Eclipse run configuration. By default it will not run your application from any jars you create, nor will it by default move resources into your classpath. The default eclipse run will build your classes (and only your classes) into a separate directory and run from there. – Trevor Brown May 12 '15 at 16:04
0

The last time something like this happened to me, it was because my build did not copy the files into the jar. Might be worth doing a sanity check by listing the files in your jar:

jar tf /path/to/your.jar
Trevor Brown
  • 169
  • 3
  • I did check the contents of the jar file, and the resources were there, in the correct folders. – user26830 May 12 '15 at 15:46
  • Is the jar definitely on your classpath? Another thing I've seen devs do is run their code in a way that they think they're running out of the jar, but in actuality they're running off the filesystem somewhere. – Trevor Brown May 12 '15 at 15:49
  • @user26830 Please show the output of the `jar tf` command. I would be somewhat surprised if the JAR actually contains a `res/` folder, which would mean you should be using `"images/bg.jpg"` or so. – Brett Kail May 13 '15 at 21:17