简体   繁体   中英

URI is not hierarchical when loading file from .jar

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM