简体   繁体   中英

ClassLoader.getSystemResourceAsStream not working when running on tomcat

I am loading a configfile using ClassLoader.getSystemResourceAsStream. The file is placed in the src folder of my GWT application. It ends up in war/WEB-INF/classes . I copy the war folder to tomcat under webapps/MyApp . When running the application, getSystemResourceAsStream throws an exception. When running under Jetty it works fine.

Try:

getClass().getClassLoader().getResourceAsStream();

It will definitely work; I also had same type of problem. This question describes why you are having that problem.

Try getClass().getResourceAsStream() instead. You need to call this method to access the class loader for your web application. The method you're calling uses the system class loader, which I believe is only going to contain the classes for the web container itself.

This should work:

Thread.currentThread().getContextClassLoader().getResourceAsStream( "relative/path" );

Note that relative/path is a path relative to war/WEB-INF/classes Eg if your file is war/WEB-INF/classes/resources/my.properties then use "resources/my.properties"

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