Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
598 views
in Technique[技术] by (71.8m points)

java - External log4j.xml file

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...