External log4j.xml file

后端 未结 9 619
忘了有多久
忘了有多久 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 22:43

    The simplest path configuration for ``log4j2is using thestatic blocksettinglog4j.configurationFile`:

    public class MyClass {
    
        static {
            System.setProperty("log4j.configurationFile", "./config/log4j2.xml");
        }
    
        protected final transient Logger logger =  LogManager.getLogger(IDOLTracker.class);
    
        public static void main(String[] args) {
             logger.info("");
        }
    }
    

    Then the structure could be as:

    ProgramFolder
    |---- /config/log4j2.xml
    |---- MyClass.jar

    When you run your jar, it will look outside the jar for the xml file.

    0 讨论(0)
  • 2020-12-12 22:44

    "-jar" only uses the classpath inside the executable jar, and -cp is ignored. Adding "." to the classpath in the executable jar should allow log4j.xml to be fount.

    0 讨论(0)
  • 2020-12-12 22:45

    For log4j2, use configurationFile option:

    java -Dlog4j.configurationFile=/path/to/log4j2.xml -jar ...
    

    http://logging.apache.org/log4j/2.0/manual/configuration.html

    0 讨论(0)
  • 2020-12-12 22:45

    I had problems using log4j with Sun's JDK and automatic configuration.

    You can use this:

    String filename = System.getProperty("log4j.configuration");
    DOMConfigurator(filename);
    

    before using Logger.

    0 讨论(0)
  • 2020-12-12 22:48

    java -cp "path/to/your/log4jxml:path/to/yourjar.jar" your.package.MainClass

    The log4j.xml in directory "path/to/your/log4jxml" will overwrite the log4j.xml file in "path/to/yourjar.jar".

    Please refer to Setting multiple jars in java classpath.

    0 讨论(0)
  • 2020-12-12 22:49

    When using the -jar switch to launch an executable jar file, the classpath is obtained from the jar file's manifest. The -cp switch, if given, is ignored.

    Jeff Storey's answer is going to be the easiest solution. Alternatively, you could add a Class-Path attribute to the jar file's manifest.

    EDIT Try enabling log4j debugging, and making the path to log4j.xml a fully-qualified URL. For example:

    java -Dlog4j.debug -Dlog4j.configuration=file:/path/to/log4j.xml -jar ...
    
    0 讨论(0)
提交回复
热议问题