load file within a jar

前端 未结 3 858
名媛妹妹
名媛妹妹 2020-11-27 19:18

I need to package a configuration file within a jar. the configuration file is under the root of the jar file. However I got the following error:

Cau

相关标签:
3条回答
  • 2020-11-27 20:00

    You should use getResourceAsStream() instead. If the file is embedded in your JAR the URI is most likely bundle:// URI

    InputStream is = this.getClass().getResourceAsStream("my.conf");
    
    0 讨论(0)
  • 2020-11-27 20:04

    Why do you need a file? IF you need to read the config use

    Class.getResourceAsStream("/my.conf");
    

    This will need only to be the file in the one folder with the root of your package( the same as in the root of the jar)

    0 讨论(0)
  • 2020-11-27 20:24

    The file should be in the same package as the MyClass. I just realized you are creating a File object. Instead try using getResourceAsStream(). This is the right way if you want to read the contents from a classpath resource. Here is the example.

    0 讨论(0)
提交回复
热议问题