0

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

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.

Ryan
  • 727
  • 2
  • 14
  • 31

1 Answers1

3

This is because /resources/levels is a directory not a file. Check the answers for this question

Community
  • 1
  • 1
AJA
  • 456
  • 3
  • 12
  • And I use it as a directory to access it's sub directories and eventually it's files, but I need the directory-like structure for indexing reasons. And I'm still unsure how I would actually use `InputStream` for this. Would code on how I use `initSource` help? – Ryan Dec 29 '16 at 20:23
  • Try using `initSource = new File(getClass().getResource("/resources/levels").getFile())` – AJA Dec 30 '16 at 07:29
  • This still works in Eclipse, but I still get an error when run as a jar from Command Prompt. However, this time the error is a `NullPointerException`. – Ryan Jan 01 '17 at 08:39
  • I managed to find a way around the problem by using a .zip file and a `ZipInputStream`. Thank you for your help though. – Ryan Jan 01 '17 at 21:56