How to read a directory from the runtime classpath?

后端 未结 3 1861
后悔当初
后悔当初 2021-01-04 08:51

My Java application needs to be able to find a myconfig/ directory which will be bundled inside the same JAR:

myjar.jar/
    com/
        me/
           


        
3条回答
  •  别那么骄傲
    2021-01-04 09:23

    The trick seems to be that the class loader can find directories in the classpath, while the class can not.

    So this works

    this.getClass().getClassLoader().getResource("com/example/foo/myconfig");
    

    while this does not

    this.getClass().getResource("myconfig");
    

提交回复
热议问题