Reputation: 996
So I've been trying for quite a while to deal with an issue I have after converting my java project(game) into a runnable jar. My issue has to do with the way I'm loading my resources. As I've read in many other answers that one possible way to deal with image loading for example when dealing with a runnable jar is to use ImageIO.read(getClass().getResourceAsStream(path);
This works perfectly.
But my issue is that in a particular folder I have 300 pngs I need to load and they way I've been dealing with this so far is by using a File dir
object representing the directory and then iterating through all the files in that directory by using dir.listFiles()
, extracting their path and loading them normally with ImageIO
. The problem is that this method throws an exception when launching the jar.
Although I could theoretically hard code every single image loading for all the 300 pngs I would like to know if there is a way around this obstacle.
Many thanks and sorry for the long post!
Alex
(Update) My project hierarchy in eclipse look like this:
Game // the project
>src //source code
>main //main package
>loader // the loader file
>res // resources
>images // the folder containing the 300pngs
Upvotes: 2
Views: 1184
Reputation: 24630
Get the jar you are running. Hints are here: Get name of running Jar or Exe.
Access the JAR with JarFile/JarEntry: http://docs.oracle.com/javase/7/docs/api/java/util/jar/JarFile.html#entries().
Upvotes: 1