Loading a configuration file from the classpath

前端 未结 4 1809
眼角桃花
眼角桃花 2020-12-17 04:43

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

相关标签:
4条回答
  • 2020-12-17 04:52

    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?

    0 讨论(0)
  • 2020-12-17 04:55

    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.

    0 讨论(0)
  • 2020-12-17 05:14

    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"); 
    
    0 讨论(0)
  • 2020-12-17 05:15

    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");
    
    0 讨论(0)
提交回复
热议问题