0

I currently have an Eclipse project that works perfectly when run within Eclipse. However, once I export it it fails to access the res folder where images are stored. I think that folder may not even be getting into the .jar because I opened it up and I couldn't find it.

I've uploaded a series of screenshots detailing how I built my project to this address: https://imageshack.com/a/Qwk4/1

From what I've searched online, this seems to be the proper way to export the project, so any help would be very helpful in solving this mystery.

Jon McClung
  • 1,619
  • 20
  • 28
  • None of those actually show where `bg.png` is. – nitind Apr 19 '15 at 21:04
  • You should take a look at http://stackoverflow.com/questions/2073250/eclipse-export-to-jar-and-include-resource-files-ant – Mathias Kogler Apr 19 '15 at 21:05
  • It's inside the res folder. I have added a new picture displaying that folder's contents, and you can see the path in the screenshot – Jon McClung Apr 19 '15 at 21:06
  • I did take a look there. I couldn't figure what Maven or Ant were or how they would help me. The best answer suggested using `Class.getResource("/filename");` but that yields this error: "Cannot make a static reference to the non-static method getResource(String) from the type Class". If I try to add res as a source folder, I get this error: "Build path contains duplicate entry: 'res' for project 'game15'". If you have more specific help for what part of that question I should be implementing in my project, please elaborate. – Jon McClung Apr 19 '15 at 21:13
  • Use the class of the object from which you're calling the method, instead of Class -- `this.getClass().getResource("/res/filename")`. If res is not in the classpath at runtime, then you need to use its name as part of the filename. An alternate way is to put the resource in the SAME directory as the class file of the class from which you are loading it, then just use "filename" without any slashes". A 2nd alternate is to put it in a "res" folder in the class file location, then load from "res/filename" (note: no slash at the start). – arcy Apr 19 '15 at 21:31

1 Answers1

0

Your Eclipse project has the res folder in the classpath. This makes it possible to load resources from there as /file . But when you are running the jar this location is not in the classpath.

Either move the resource files to the root of the project/jar or do resource loading from /res instead of / (and make sure the directory is included into the jar).

Alex Nevidomsky
  • 668
  • 7
  • 14
  • I used your solution, but it only kinda worked. I still couldn't get it to package the res folder into the jar, but I managed to open up the jar and create a res folder and fill it with the stuff I needed. – Jon McClung Apr 20 '15 at 00:53
  • You can manually select what goes into jar when exporting project as jar in Eclipse. A simple way would be to put resources right into the source code tree, then Eclipse will pick them up automatically. – Alex Nevidomsky Apr 20 '15 at 08:01
  • But that doesn't work for me. Even if I put everything right in the bin folder it just leaves it behind – Jon McClung Apr 21 '15 at 12:02
  • If you are confused by Eclipse, the most reliable way is to put resource files (images) right besides your Game.java file that uses them (and use path to resource without the /). This way the files will go into the same place in the jar. – Alex Nevidomsky Apr 21 '15 at 15:09
  • But generally speaking, everything is quite simple, you just need to understand the mechanics: there is always classpath when you run your app, you just need to know what it is. If you don't specify it in the command line, it's the root of your jar. That's where it will be looking for the file, and in the same place as the .class file if you don't use / at the beginning. – Alex Nevidomsky Apr 21 '15 at 15:09