java 从 tomcat webapps 文件夹中读取属性文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3665547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading Property file from tomcat webapps folder
提问by Renisha
I have placed a property file in the webaaps folder of tomcat but not able to access it. My code is
我在 tomcat 的 webaaps 文件夹中放置了一个属性文件,但无法访问它。我的代码是
InputStream is = ClassLoader.getSystemResourceAsStream("database.properties");
if(is == null){
is = ClassLoader.getSystemClassLoader().getResourceAsStream("database.properties");
}
if(is == null){
is = ClassLoader.getSystemClassLoader().getResourceAsStream("/database.properties");
}
if(is == null){
is = ClassLoader.getSystemClassLoader().getResourceAsStream("/webapps/database.properties");
}
if(is == null){
is = ClassLoader.getSystemClassLoader().getResourceAsStream("webapps/database.properties");
}
Still , I am getting input stream as null.
尽管如此,我还是将输入流设为空值。
回答by Pascal Thivent
The getSystemResourceAsStream
method returning null
means the resource was not found on the classpath at the specified location. And this makes sense since $TOMCAT_HOME/webapps
isn't on the classpath.
在getSystemResourceAsStream
返回的方法null
手段的资源没有在指定位置的类路径中。这是有道理的,因为$TOMCAT_HOME/webapps
不在类路径上。
So either put that file in a directory that is on the classpath of your application (and if you want to have it outside the war, then you'll have to put it in $CATALINA_HOME/lib
- with all the implied consequences) or change your approach (use JNDI to store the properties, use an absolute path, etc).
因此,要么将该文件放在应用程序类路径上的目录中(如果您想将其置于War之外,那么您必须将其放入$CATALINA_HOME/lib
- 以及所有隐含的后果)或更改您的方法(使用JNDI 来存储属性,使用绝对路径等)。
References
参考
- Apache Tomcat 6.0 documenation
- Apache Tomcat 6.0 文档
回答by Benoit Courtine
To access properties files by the classpath, I put theses files in "webapp/WEB-INF/classes".
为了通过类路径访问属性文件,我将这些文件放在“webapp/WEB-INF/classes”中。
Doing like this is standard, and is a good way to avoid absolute path problems.
这样做是标准的,并且是避免绝对路径问题的好方法。
If your project has a standard Maven structure, it is managed by Maven :
如果您的项目具有标准的 Maven 结构,则它由 Maven 管理:
- sources are in "src/main/java"
- properties are in "src/main/resources"
- 源在“src/main/java”中
- 属性在“src/main/resources”中
When Maven package the web application, classes are compiled in "webapp/WEB-INF/classes" and resources are copied to the same directory.
Maven 打包web 应用程序时,类被编译在“webapp/WEB-INF/classes”中,并将资源复制到同一目录中。
回答by amorfis
You can't access file that is outside your web application this way. This file is not in your classpath, so you can't get it by "ClassLoader". Put it to your classpath (inside *.war) or access it like this:
您无法通过这种方式访问 Web 应用程序之外的文件。此文件不在您的类路径中,因此您无法通过“ClassLoader”获取它。把它放到你的类路径(在 *.war 里面)或者像这样访问它:
File f = new File("/absolute/path/to/your/file");
InputStream is = new FileInputStream(f);
But still tomcat/webapps
is not a good place for properties file. Maybe user's home directory? Then you could access it like this:
但仍然tomcat/webapps
不是属性文件的好地方。也许用户的主目录?然后你可以像这样访问它:
String home = System.getProperty("user.home");
File f = new File(home + "/yourfile.properties");
回答by PraveenK
Need to update classpath with folder name that contains this file. I did that it worked. I did in Eclipse, tomcat and standalone java
需要使用包含此文件的文件夹名称更新类路径。我做到了,它奏效了。我在 Eclipse、tomcat 和独立的 java 中做过
java -cp .:$CLASSPATH:some/location/your/config yourclass
java -cp .:$CLASSPATH:some/location/your/config yourclass