How can I locate a non-Java resource in a WAR at runtime?

拟墨画扇 提交于 2019-12-01 09:25:14

You don't want to use the System cloassloader - that would typically only find resources that are part of Tomcat or the JRE.

You want to load the resource from the classloader that was used to load your WAR file. There's a couple of ways to do that, but the official way would be to get access to the ServletContext (javadoc) and call either getResource or getClassLoader().getResource on that.

If you don't have the ServletContext available, then you can "cheat" by using the classloader for a class that you know came from your WAR file. Something like

this.getClass().getClassLoader().getResource("xyz");

where this is your class that's been deployed inside your WAR file.

have you tried getClass().getClassLoader().getResource() or getResourceAsStream() ?if its in the classpath, these should get it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!