how to add and read resource file from jar

前端 未结 2 693
生来不讨喜
生来不讨喜 2020-12-11 11:09

I have hibernate.cfg.xml and test.txt in the path which i read by java program. Now when i created the jar using maven those files were not present. So i read that i should

相关标签:
2条回答
  • 2020-12-11 11:24

    getClass().getResourceAsStream(name) searches for the resource in the same dir as the class for which this method is called is in.

    For instance, you have class A and resource test.txt in the same dir the you call getClass().getResourceAsStream("test.txt"). If it's located in some subdir, you need to express that in name: getClass().getResourceAsStream("subdir/test.txt").

    I haven't tested that, but looking in dirs above current should be possible with: getClass().getResourceAsStream("../test.txt").

    0 讨论(0)
  • 2020-12-11 11:37

    Looking at this article, Does getClass().getResourceAsStream("/test.txt") make a difference?

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