开发者

Why is this classpath resource null?

I have a Maven project structured like this:

  • src/main/java/test/App.java
  • src/main/resources/config/message.txt

Inside App.java, I have this line:

InputStream is = Thread.currentThread()
    .getContextClassLoader()
    .getResourceAsStream("/config/message.txt");

I've tried every variation of getting the resource that I could find on Stackoverflow and G开发者_如何学Pythonoogle, but it always seems to return null when I run mvn exec:java -Dexec.mainClass=test.App. What am I missing here?


Try:

InputStream is = test.App.class.getResourceAsStream("/config/message.txt");

or simply:

InputStream is = getClass().getResourceAsStream("/config/message.txt");


You can try this:

InputStream is = this.getClass().getResourceAsStream("/config/message.txt");


My bet:

InputStream is = App.class.
    getResourceAsStream("../../resources/config/message.txt");

I don't understand what you want to do with the currentThread() here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜