So I have a problem with loading a file when running my program from a jar file. I am aware that questions very similar to this exist but I can find none that work for me or do what I need. I would like to load an object file from a folder in my jar but when I do I get the following error:
Exception in thread "main" java.lang.IllegalArgumentException: URI is not hierarchical
at java.io.File.<init>(Unknown Source)
at bbsource.BouncyBallV5.loadLevels(BouncyBallV5.java:370)
at bbsource.BouncyBallV5.<init>(BouncyBallV5.java:243)
at BBDriver.main(BBDriver.java:18)
Line 370 is as follows
initSource = new File(getClass().getResource("/resources/levels").toURI());
I have no problems with this line when running from Eclipse but I am aware that things act differently in jar files. The folder hierarchy is:
- src
- resources
- levels
- tier_one
- Level1.cbbl
- Level2.cbbl
- tier_two
- Level1.cbbl
- tier_one
- levels
- resources
levels is a directory that it will not let me access, and I'm not sure how to get it to work. I have seen suggestions such as using InputStream
but I'm not sure how to use that and still treat it as a file from which I can read objects.
Any help would be appreciated.