How to reference a resource file correctly for JAR and Debugging?

前端 未结 7 485
不思量自难忘°
不思量自难忘° 2020-12-02 21:58

I have a nasty problem referencing resources when using a Maven project and Jar files...

I have all my resources in a dedicated folder /src/main/resources which is p

7条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-02 22:44

    If you add the resources directory in the jar file (so it is under the /resources folder in the jar, and if /src/main is in your build path in eclipse, then you should be able to reference your file as:

    getClass().getResource("/resources/filename.txt");  
    

    Which should work in both environments.

提交回复
热议问题