在Java中访问基础包中的资源?

Kon*_*ner 1 java resources properties

我不喜欢让我的属性文件进入resources/mypackage/.../somepackage/resourcefile.properties,而宁愿让它们进入resources/resourcefile.properties.在没有的情况下,这是否可行:

  1. 被迫写MyApplication.class.getResourceAsStream("../../../../../../../../resources/resourcefile.properties")
  2. 使用时它在Tomcat中不起作用SomeClass.getClassLoader().getResourceAsStream("resourcefile.properties")

Bar*_*end 5

添加一个前导斜杠来指定根包:

SomeClass.getClassLoader().getResourceAsStream("/resourcefile.properties")
Run Code Online (Sandbox Code Playgroud)