从类路径加载配置文件
我正在努力使我的 Java 应用程序易于部署到其他计算机,并正在编写一个 ant 脚本来执行此操作,一切进展顺利。
我在加载 jar 清单文件中指定的类路径中列出的资源时遇到问题。
文件夹结构如下所示:
/MyProgram.jar
/lib/<dependencies>
/config/configuration.xml
我一生都无法使用 ClassLoader 访问 configuration.xml 文件。它以及所有依赖项都明确列在清单文件的类路径条目中。
我尝试了以下多种变体:
this.xml = Thread.currentThread().getContextClassLoader()
.getResourceAsStream(xmlName);
this.xml = this.getClass().getResourceAsStream(xmlName);
将 xmlName 作为所有以下值的字符串:
"config/configuration.xml"
"configuration.xml"
"config.configuration.xml"
与此相关,我在 config 目录中还有一个 log4j.properties 文件。我如何让 log4j 拾取它?其他参考文献说它只需要位于类路径中,并且它也在 jar 的清单文件中明确命名。有人能指出我正确的方向吗?
更新:
以下是类路径中的实际条目:
Class-Path: <snip dependencies> config/configuration.xml config/log4j.properties
I'm working on making my Java app easy to deploy to other computers and am writing an ant script to do so, that is going fine.
I'm having trouble loading resources that are listed in the classpath named in the manifest file of the jar.
Folder structure looks like this:
/MyProgram.jar
/lib/<dependencies>
/config/configuration.xml
I can not for the life of me access the configuration.xml file using the ClassLoader. It, along with all the dependencies are listed explicitly in the Class-Path entry to the manifest file.
I've tried many variants of the following:
this.xml = Thread.currentThread().getContextClassLoader()
.getResourceAsStream(xmlName);
this.xml = this.getClass().getResourceAsStream(xmlName);
With xmlName as a string of all the following values:
"config/configuration.xml"
"configuration.xml"
"config.configuration.xml"
Related to this, I also have a log4j.properties file in the config directory. How do I get log4j to pick it up? Other references say it just needs to be in the classpath, and it too is explicitly named in the jar's manifest file. Can someone point me in the right direction?
Update:
Here are the actual entries from Class-Path:
Class-Path: <snip dependencies> config/configuration.xml config/log4j.properties
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
类路径条目应该是目录或 jar 文件,而不是单个文件。尝试更改类路径以指向配置目录而不是单个配置文件。
更好的方法是将您的配置目录包含在 MyProgram.jar 中。这将防止您需要将其专门添加到类路径中。
Classpath entries should either be directories or jar files, not individual files. Try changing your classpath to point to the config directory instead of the individual config files.
Better yet would be to just include your config directory in MyProgram.jar. This would prevent you from needing to add it specifically to the classpath.
据我所知,log4j.xml 应该位于类路径的根目录下。
而且,您可以使用下面的代码脚本读取配置文件。并且 config 目录应该位于您的类路径中。
As far as I know, log4j.xml should be at root of your classpath..
and also, you can read your configuration file with below code script. and config directory should be at your classpath.
您可以在启动应用程序时使用
log4j.configuration
系统属性:请参阅 http://logging.apache.org/log4j/1.2/manual.html“默认初始化过程”下。
关于未拾取的其他配置文件,您的Manifest.mf文件是什么样的? 内容?
您是否在使用Manifest.mf 文件中的
You can use the
log4j.configuration
system property when you startup your application:See http://logging.apache.org/log4j/1.2/manual.html under "Default Initialization Procedure".
Regarding the other configuration files not being picked up, what does your Manifest.mf file looks like? Are you using something like
in your Manifest.mf file?
关于 log4j :如果您希望它使用与默认配置文件不同的配置文件,您可以使用
此静态方法的变体接受 URL、属性或文件名。
还有
XML 文件。
Regarding log4j : If you want it to use a different config file from the default, you can use
Variants of this static method accept URL, Properties or file name.
There is also
for the XML files.