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
The simplest path configuration for ``log4j2is using the
static blocksetting
log4j.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.
"-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.
For log4j2, use configurationFile option:
java -Dlog4j.configurationFile=/path/to/log4j2.xml -jar ...
http://logging.apache.org/log4j/2.0/manual/configuration.html
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.
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.
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 ...