[英]Property file in Tomcat\bin folder getting java.util.MissingResourceException
I want to read a properties file from Tomcat\\bin
folder using ResourceBundle
. 我想使用
ResourceBundle
从Tomcat\\bin
文件夹中读取属性文件。 When I put my properties file in project's root folder, I am able to read this file. 当我将我的属性文件放在项目的根文件夹中时,我能够读取此文件。 But when I put it in
Tomcat\\bin
folder, I'm getting java.util.MissingResourceException
. 但是当我把它放在
Tomcat\\bin
文件夹中时,我得到的是java.util.MissingResourceException
。 How is this caused and how can I solve it? 这是怎么造成的,我该如何解决?
You need to put it in the classpath. 您需要将它放在类路径中。 The
Tomcat/bin
folder is not part of the webapp's default runtime classpath. Tomcat/bin
文件夹不是webapp的默认运行时类路径的一部分。 If you intend to add an external folder to the classpath of a webapp running in Tomcat, then you need to specify the path to that folder in the shared.loader
property of the Tomcat/conf/catalina.properties
file. 如果要将外部文件夹添加到在Tomcat中运行的webapp的类路径中,则需要在
Tomcat/conf/catalina.properties
文件的shared.loader
属性中指定该文件夹的路径。 I would however choose a different folder than the Tomcat/bin
. 然而,我会选择与
Tomcat/bin
不同的文件夹。
Unrelated to the problem, the ResourceBundle
API is meant to read resourcebundle files (with localized content so that you can internationalize your app), not to read simple properties files (with configuration settings). 与此问题无关 ,
ResourceBundle
API旨在读取resourcebundle文件(具有本地化内容以便您可以使应用程序国际化),而不是读取简单属性文件(使用配置设置)。 For that you should be using java.util.Properties
instead and not abuse the java.util.ResourceBundle
. 为此,您应该使用
java.util.Properties
而不是滥用java.util.ResourceBundle
。
Properties properties = new Properties();
properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("/config.properties"));
// ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.