从tomcat中的webapp检索属性文件时出现问题

时间:2021-09-20 00:32:03

I've developed a web application that worked fine in JBoss 4. Now, I need to make it work in Tomcat 6, but I'm having trouble to access some properties file. I use the following code to read read these files:

我开发了一个在JBoss 4中运行良好的Web应用程序。现在,我需要让它在Tomcat 6中运行,但是我无法访问某些属性文件。我使用以下代码来读取这些文件:

InputStream is = Thread.currentThread().getContextClassLoader()
                .getResourceAsStream(fileName);
if (is == null) {
    throw new StartupError("Error loading " + fileName);
}

properties.load(is);

As I've said before, it works fine in JBoss 4. But when I deploy my app in tomcat, it doesn't find the file, assigning null to 'is' variable, causing the StartupError to be thrown. The file is located at WEB-INF/config directory, and the webapp is deployed as a war.

正如我之前所说,它在JBoss 4中工作正常。但是当我在tomcat中部署我的应用程序时,它找不到文件,将null赋给'is'变量,导致抛出StartupError。该文件位于WEB-INF / config目录,webapp部署为战争。

Any solution for this problem?

解决这个问题的任何方法?

Thanks, Alexandre

2 个解决方案

#1


Put the properties files in WEB-INF/classes.

将属性文件放在WEB-INF / classes中。

Or include them in the root of one of your webapp Jar files, although this makes it harder to edit them. This is good if you're selecting properties within a build script and don't want to edit them once deployed.

或者将它们包含在您的一个webapp Jar文件的根目录中,尽管这使得编辑它们变得更加困难。如果您在构建脚本中选择属性并且在部署后不想编辑它们,那么这很好。

#2


I assume that Tomcat does not add WEB-INF/config into your webapp classpath.

我假设Tomcat没有将WEB-INF / config添加到您的webapp类路径中。

from http://tomcat.apache.org/tomcat-6.0-doc/class-loader-howto.html

WebappX - A class loader is created for each web application that is deployed in a single Tomcat 6 instance. All unpacked classes and resources in the /WEB-INF/classes directory of your web application archive, plus classes and resources in JAR files under the /WEB-INF/lib directory of your web application archive, are made visible to the containing web application, but to no others.

WebappX - 为部署在单个Tomcat 6实例中的每个Web应用程序创建一个类加载器。 Web应用程序归档的/ WEB-INF / classes目录中的所有解压缩的类和资源,以及Web应用程序归档的/ WEB-INF / lib目录下的JAR文件中的类和资源对包含的Web应用程序可见,但没有其他人。

#1


Put the properties files in WEB-INF/classes.

将属性文件放在WEB-INF / classes中。

Or include them in the root of one of your webapp Jar files, although this makes it harder to edit them. This is good if you're selecting properties within a build script and don't want to edit them once deployed.

或者将它们包含在您的一个webapp Jar文件的根目录中,尽管这使得编辑它们变得更加困难。如果您在构建脚本中选择属性并且在部署后不想编辑它们,那么这很好。

#2


I assume that Tomcat does not add WEB-INF/config into your webapp classpath.

我假设Tomcat没有将WEB-INF / config添加到您的webapp类路径中。

from http://tomcat.apache.org/tomcat-6.0-doc/class-loader-howto.html

WebappX - A class loader is created for each web application that is deployed in a single Tomcat 6 instance. All unpacked classes and resources in the /WEB-INF/classes directory of your web application archive, plus classes and resources in JAR files under the /WEB-INF/lib directory of your web application archive, are made visible to the containing web application, but to no others.

WebappX - 为部署在单个Tomcat 6实例中的每个Web应用程序创建一个类加载器。 Web应用程序归档的/ WEB-INF / classes目录中的所有解压缩的类和资源,以及Web应用程序归档的/ WEB-INF / lib目录下的JAR文件中的类和资源对包含的Web应用程序可见,但没有其他人。