Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying Log4j2 Configuration File When Using Executable JAR

Tags:

I am having trouble specifying the Log4j2 config file location when using an executable JAR file. It works fine if I separate all the JARs, but when I try to combine them into one executable JAR file, for some reason the log4j2.xml file isn't picked up from the command line.

I've tried these two methods of specifying the location:

java -Djava.libary.path=..\bin -cp ..\config -jar MyApplication.jar

java -Djava.libary.path=..\bin -Dlog4j.configurationFile=..\config\log4j2.xml -jar MyApplication.jar

Neither of those are working. I've also tried adding the directory containing the config file to the classpath in the JAR's manifest file:

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.9.2
Created-By: 1.7.0_21-b11 (Oracle Corporation)
Main-Class: com.abc.MyApplication
Class-Path: ../config/

I haven't had success with this method either. Any ideas what I might be doing wrong?

Thanks in advance for any help!


EDIT

Ah, I believe I mistook the problem. Originally, this was the error I was seeing in the command line output:

ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.

But at some point while I was changing things, the error message changed without my realizing it to this:

ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...

So what I figured out was that even though the executable JAR I was building was including the log4j-core-2.1.jar and log4j-api-2.1.jar JARs inside of it and in the MANIFEST file's classpath, there was a problem. The way I wrote my ant file to combine the libraries into the single JAR I was creating was successfully copying over the directories and class files, but was not copying over the other types for some reason, which are also obviously necessary (e.g. Log4j-config.xsd, Log4j-events.dtd, etc.).

To fix this, I changed the way I was merging the JARs in my Ant build file to this:

<jar destfile="${dist}/${jarName}" basedir="${classes}" 
    excludes=".svn">

    <!-- Merge this JAR with all the JARs in the lib directory, so that
    we are only creating one distribution JAR that includes all the
    libraries that you need. -->
    <fileset dir="${classes}" includes="**/*.class" />
    <zipgroupfileset dir="${lib}" includes="**/*.jar" />

    <!-- Specify the manifest file of the JAR -->
    <manifest>
        <attribute name="Main-Class" value="com.abc.MyApplication"/>
        <attribute name="Class-Path" value=". ${manifest.classpath}"/>
    </manifest>
</jar>

And that fixed the issue and copied over all files from the JARs into my newly created JAR.

Once this issue was resolved, the second of the commands I posted above worked for specifying the location of the configuration file. (As noted by @rewolf below, the first command won't work because the classpath specified in the MANIFEST of the JAR overrides any classpath specified on the command line.

Thanks for your responses, they definitely helped me get on the right path toward figuring out my mistakes.

like image 835
Steph Avatar asked Feb 18 '15 02:02

Steph


People also ask

What is log4j2 configuration file?

Configuration File Formats Log4j will load Java properties and YAML, JSON, and XML configuration files. It identifies the file format by examining the file extension. Java properties — .properties. YAML — .yaml or .yml. JSON — .json or .jsn.

Where is log4j2 properties file?

By default, Log4J 2 looks for a properties file with the name log4j2. properties in the classpath. In a Spring Boot application, the log4j2. properties file will typically be in the resources folder.


1 Answers

Something that isn't explained very well/obviously in the Java documentation is that if you're using an executable Jar, it will only use the Class-Path as specified in the Manifest file. It will not listen to the -cp or --classpath arguments.

-Dlog4j.configurationFile=directory/file.xml

should definitely work though. I'm assuming you're running on Windows given your slash direction. Are you sure you are running it from the correct relative directory?

Update

I just tried it in Windows with no problems. I used the following manifest:

Manifest-Version: 1.0
Built-By: andrew.flower
Build-Jdk: 1.7.0_67
Class-Path: lib/log4j-api-2.1.jar lib/log4j-core-2.1.jar
Created-By: Apache Maven 3.2.3
Main-Class: com.andrew_flower.test.Log4jTest
Archiver-Version: Plexus Archiver

The Log4j2 jars are located in a lib/ directory and the log4j2.xml is in the conf/ directory. I executed the following command, and it found the config successfully.

java -Dlog4j.configurationFile=conf\log4j2.xml -jar log4j2test1-1.0-SNAPSHOT.jar
like image 94
rewolf Avatar answered Oct 27 '22 00:10

rewolf