从类路径加载配置文件

发布于 2024-10-14 19:19:58 字数 903 浏览 6 评论 0原文

我正在努力使我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

掀纱窥君容 2024-10-21 19:19:58

类路径条目应该是目录或 jar 文件,而不是单个文件。尝试更改类路径以指向配置目录而不是单个配置文件。

this.xml = Thread.currentThread().getContextClassLoader()
             .getResourceAsStream("config.xml");

更好的方法是将您的配置目录包含在 MyProgram.jar 中。这将防止您需要将其专门添加到类路径中。

this.xml = Thread.currentThread().getContextClassLoader()
             .getResourceAsStream("/config/configuration.xml");

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.

this.xml = Thread.currentThread().getContextClassLoader()
             .getResourceAsStream("config.xml");

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.

this.xml = Thread.currentThread().getContextClassLoader()
             .getResourceAsStream("/config/configuration.xml");
荒芜了季节 2024-10-21 19:19:58

据我所知,log4j.xml 应该位于类路径的根目录下。

而且,您可以使用下面的代码脚本读取配置文件。并且 config 目录应该位于您的类路径中。

this.xml = this.getClass().getResourceAsStream("/config/configuration.xml"); 

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.

this.xml = this.getClass().getResourceAsStream("/config/configuration.xml"); 
Oo萌小芽oO 2024-10-21 19:19:58

您可以在启动应用程序时使用 log4j.configuration 系统属性:

java -Dlog4j.configuration=config/log4j.properties MyApp

请参阅 http://logging.apache.org/log4j/1.2/manual.html“默认初始化过程”下。

关于未拾取的其他配置文件,您的Manifest.mf文件是什么样的? 内容?

Class-Path: config/configuration.xml lib/yourLibOne.jar lib/blah.jar

您是否在使用Manifest.mf 文件中的

You can use the log4j.configuration system property when you startup your application:

java -Dlog4j.configuration=config/log4j.properties MyApp

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

Class-Path: config/configuration.xml lib/yourLibOne.jar lib/blah.jar

in your Manifest.mf file?

凉风有信 2024-10-21 19:19:58

关于 log4j :如果您希望它使用与默认配置文件不同的配置文件,您可以使用

org.apache.log4j.PropertyConfigurator.configure(...)

此静态方法的变体接受 URL、属性或文件名。

还有

org.apache.log4j.xml.DOMConfigurator

XML 文件。

Regarding log4j : If you want it to use a different config file from the default, you can use

org.apache.log4j.PropertyConfigurator.configure(...)

Variants of this static method accept URL, Properties or file name.

There is also

org.apache.log4j.xml.DOMConfigurator

for the XML files.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文