External log4j.xml file

后端 未结 9 620
忘了有多久
忘了有多久 2020-12-12 22:32

I am trying to run a jar with the log4j.xml file on the file system outside the jar like so:

java -jar MyJarName.jar -cp=/opt/companyName/pathToJar/ log4j.co         


        
相关标签:
9条回答
  • 2020-12-12 23:02

    you can define a default properties file in the jar. if no custom properties is defined you can use this default file.if a custom property is defined you can override the default property.

    myjar.jar file contains log4j.default.configuration

    you can run your program with this parameters to start your application

    java  -jar -Dlog4j.configuration=log4j.properties  target\yourfile-v01_000_01_c002.jar
    

    Example code

    public static void main(String[] args) {
        String filename = System.getProperty("log4j.configuration");
        if(null==filename||filename.trim().equals("")) {
            logger.info("Using default log4j configuration log4j.default.properties.");
            logger.info("If you would like to change the default configuration please add following param before startup -Dlog4j.configuration=<log4jfile>");
            PropertyConfigurator.configure( Main.class.getResourceAsStream("/log4j.default.properties"));
        } else {
            File file = new File(filename);
            if(!file.exists()) System.out.println("It is not possible to load the given log4j properties file :"+file.getAbsolutePath());
            else PropertyConfigurator.configure(file.getAbsolutePath());
    
        }
    }
    
    0 讨论(0)
  • 2020-12-12 23:04

    this works:

    java -jar -Dlog4j.configuration="file:d:\log4j.xml" myjar.jar
    
    0 讨论(0)
  • 2020-12-12 23:07

    Have you tried java -Dlog4j.configuration=/path/to/log4j.xml -jar <rest-of-args>

    0 讨论(0)
提交回复
热议问题