getResourceAsStream没有在webapp中加载资源 [英] getResourceAsStream not loading resource in webapp

查看:132
本文介绍了getResourceAsStream没有在webapp中加载资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用驻留在TOMCAT_HOME / common / lib中的库的Web应用程序。此库在类路径的根(在名为ApplicationConfig的类中)中查找属性文件:

I have a web application that uses a library which resides in TOMCAT_HOME/common/lib. This library looks for a properties file at the root of the classpath (in a class called ApplicationConfig):

ApplicationConfig.class.getResourceAsStream("/hv-application.properties");

我的Tomcat Web应用程序包含此属性文件。它在WEB-INF / classes中,它是classpath的根本吗?但是,在运行时,当它尝试加载属性文件时,它会抛出异常,因为它无法找到它(getResourceAsStream返回null)。

My Tomcat web application contains this properties file. It is in WEB-INF/classes, which is the root of the classpath right? However, at runtime, when it tries to load the properties file, it throws an exception because it can't find it (getResourceAsStream returns null).

一切正常我的应用程序是一个简单的独立Java应用程序。 Tomcat会导致getResourceAsStream方法采取不同的行为吗?我知道那里有很多类似的问题,但不幸的是他们都没有帮助过。谢谢。

Everything works fine if my application is a simple, standalone Java application. Does Tomcat cause the getResourceAsStream method to act differently? I know there's a lot of similar questions out there, but none of them have helped unfortunately. Thanks.

推荐答案

尝试 Thread.currentThread()。getContextClassLoader()。getResourceAsStream(/ hv- application.properties)代替。

这篇关于getResourceAsStream没有在webapp中加载资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆