41

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.configuration=log4j.xml argToJar1 argToJar2

I have also tried:

java -jar MyJarName.jar -cp=/opt/companyName/pathToJar/ log4j.configuration=/opt/companyName/pathToJar/log4j.xml argToJar1 argToJar2

The log4j.xml is file is in the same directory as the jar (/opt/companyName/pathToJar/), yet I still get the standard warning message:

log4j:WARN No appenders could be found for logger (org.apache.axis.i18n.ProjectResourceBundle).
log4j:WARN Please initialize the log4j system properly.

Is it possible to have the config file outside the jar, or do I have to package it with the jar?

TIA

javamonkey79
  • 17,443
  • 36
  • 114
  • 172

9 Answers9

55

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 ...
Community
  • 1
  • 1
Jason Day
  • 8,809
  • 1
  • 41
  • 46
18

this works:

java -jar -Dlog4j.configuration="file:d:\log4j.xml" myjar.jar
tibi
  • 657
  • 1
  • 10
  • 22
15

For log4j2, use configurationFile option:

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

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

r00tGER
  • 391
  • 2
  • 11
8

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

Jeff Storey
  • 56,312
  • 72
  • 233
  • 406
6

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

    }
}
Koray Güclü
  • 2,857
  • 1
  • 34
  • 30
  • 2
    Thank you! This works for .properties-file. If you use .xml configuration file then use DOMConfigurator instead of PropertyConfigurator. – Martin Pabst Jul 11 '16 at 14:11
2

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.

Community
  • 1
  • 1
Dayong
  • 5,614
  • 1
  • 14
  • 6
1

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.

another
  • 3,440
  • 4
  • 27
  • 34
1

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.

VlatkoB
  • 1,219
  • 1
  • 12
  • 12
0

"-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 found.

codersl
  • 2,222
  • 4
  • 30
  • 33
Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347