title: Specifying Log4j2 Configuration File When Using Executable JAR
body: <p>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 <code>log4j2.xml</code> file isn't picked up from the command line.</p>
  
  <p>I've tried these two methods of specifying the location:</p>
  
  <pre><code>java -Djava.libary.path=..\bin -cp ..\config -jar MyApplication.jar
  
  java -Djava.libary.path=..\bin -Dlog4j.configurationFile=..\config\log4j2.xml -jar MyApplication.jar
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>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/
  </code></pre>
  
  <p>I haven't had success with this method either. Any ideas what I might be doing wrong?</p>
  
  <p>Thanks in advance for any help!</p>
  
  <hr>
  
  <p>EDIT</p>
  
  <p>Ah, I believe I mistook the problem. Originally, this was the error I was seeing in the command line output:</p>
  
  <pre><code>ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
  </code></pre>
  
  <p>But at some point while I was changing things, the error message changed without my realizing it to this:</p>
  
  <pre><code>ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...
  </code></pre>
  
  <p>So what I figured out was that even though the executable JAR I was building was including the <code>log4j-core-2.1.jar</code> and <code>log4j-api-2.1.jar</code> JARs inside of it and in the <code>MANIFEST</code> 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.).</p>
  
  <p>To fix this, I changed the way I was merging the JARs in my Ant build file to this:</p>
  
  <pre><code>&lt;jar destfile="${dist}/${jarName}" basedir="${classes}" 
      excludes=".svn"&gt;
  
      &lt;!-- 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. --&gt;
      &lt;fileset dir="${classes}" includes="**/*.class" /&gt;
      &lt;zipgroupfileset dir="${lib}" includes="**/*.jar" /&gt;
  
      &lt;!-- Specify the manifest file of the JAR --&gt;
      &lt;manifest&gt;
          &lt;attribute name="Main-Class" value="com.abc.MyApplication"/&gt;
          &lt;attribute name="Class-Path" value=". ${manifest.classpath}"/&gt;
      &lt;/manifest&gt;
  &lt;/jar&gt;
  </code></pre>
  
  <p>And that fixed the issue and copied over all files from the JARs into my newly created JAR.</p>
  
  <p>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 <code>@rewolf</code> below, the first command won't work because the classpath specified in the <code>MANIFEST</code> of the JAR overrides any classpath specified on the command line.</p>
  
  <p>Thanks for your responses, they definitely helped me get on the right path toward figuring out my mistakes.</p>