i deployment of the project and launch on the server,
This is similar to the JSP / Servlet web application. In this case, you need to use ClassLoader , which is obtained as follows:
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
This has access to the paths of the all path associated with the web application in question, and you no longer depend on which parent class loader (the web application has more than one!) Loaded your class.
Then on this class loader, you just need to call getResourceAsStream() to get the classpath resource as a stream, not getSystemResourceAsStream() , which depends on how the web application starts. You do not want to depend on this, since you do not control it on external hosting:
InputStream input = classLoader.getResourceAsStream("filename.extension");
This is finally more robust than your original getSystemResourceAsStream() and Class#getResourceAsStream() approach, as others have suggested.
Balusc
source share