Loading a configuration file from the classpath

会有一股神秘感。 提交于 2019-11-29 04:54:48

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");

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"); 

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?

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!