简体   繁体   中英

Loading a configuration file from the classpath

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM