简体   繁体   中英

getResourceAsStream returns null?

I saw same posts like this but any of them could resolve my problem. I have my environment.properties which i want to access by:

InputStream inputStream = this.getClass().getResourceAsStream("/support/environment.properties");

inputStream is always receiving null. This is my project structure

MyProject
->src
-->test
--->java
---->com.desp.automation.config (package)
----->CrossAutomationConfigTest
-->main
--->resources
---->support
----->environment.properties

Do you know what's happening ? I would appreciate your help. Regards

This happens to me all the time! The actual path to the resource depends on the class loader meaning it depends on whether you are running in pure java, or a web container. I usually nail it by playing with the path to find it...things to try:

(1) /resoures/support/... (2) / main/resources/support/... (3) the above without a leading /

I think the requirement for the leading / depends on whether you are calling getResourceAsStream from a static or non-static method and whether the resources are packaged in an archive (zip/jar/war) or whether the app is unarchived into files in the deployment environment.

To avoid deployment issues, I write some code to 'discover' the path by trying getResourceAsStream until a non-null inputStream is returned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM