views:

632

answers:

4

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

+3  A: 

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

Jeff Storey
This did not work.
javamonkey79
+2  A: 

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 ...
Jason Day
I added this to the manifest and it still did not work (but I am not sure relative paths are legal):Class-Path: .
javamonkey79
Relative paths are valid. What is the command that you used?
Jason Day
java -Dlog4j.configuration=/path/to/log4j.xml -jar MyJarName.jar <rest-of-args> perhaps the space between the Class-Path: and the '.' is to blame.
javamonkey79
I will try this soon, thanks.
javamonkey79
That did it, thanks!
javamonkey79
A: 

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

Thorbjørn Ravn Andersen
A: 

this works:

java -jar -Dlog4j.configuration="file:d:\log4j.xml" myjar.jar