title: Loading a file relative to the executing jar file
body: <p>The question says it all.</p>
  
  <p>The specialty in my case is that the current working directory is not the location of the jar file but <code>c:\Windows\system32</code> (My jar file is started by windows using the right-click-menu, i want to pass a folder's path as a parameter to the jar).</p>
  
  <p>Now I want to load a configuration file called <code>config.xml</code> that is in the same folder as the jar. The purpose of the file is, of course, to provide settings for the jar. It is important for me that the xml file is <em>outside</em> of the jar file for easy editing.</p>
  
  <p>I'm having a hard time loading that file. Windows executes the line</p>
  
  <pre><code>cmd /k java -jar D:\pathToJarfile\unpacker-0.0.1-SNAPSHOT-jar-with-dependencies.jar
  </code></pre>
  
  <p>Calling the whole thing with <code>cmd /k</code> leaves the windows command prompt open so that I can see the jar's output.</p>
  
  <p>I cannot use <code>new File(".")</code> or <code>System.getProperty("user.dir")</code> for the relative path as these functions return <code>C:\Windows\system32\.</code> and <code>C:\Windows\system32</code>, respectively (which is the working folder for everything that windows executes AFAIK).</p>
  
  <p>I've had no success with <code>Launcher.class.getResourceAsStream("/../config.xml")</code> either. Since that path begins with <code>/</code>, searching starts at the root node of the jar. Going to <code>../config.xml</code> point exactly to that file, but the call returns <code>null</code>.</p>
  
  <p>Can someone point me in the right direction? I'm really stuck here. This file loading stuff really bugs me everytime...</p>
  
  <p>Requirements by myself:</p>
  
  <ul>
  <li>I don't want to hardcode the path in the java source code</li>
  <li>I don't want to pass the file's path as a parameter to the <code>java -jar</code> call (neither as a param to the <code>main(String[] args)</code> nor using <code>-Dpath=d:\...</code> to set a system property)</li>
  </ul>
  
  <hr>
  
  <p>In addition to the original problem, I had a hard time to have maven2 place <code>Class-Path: .</code> into the <code>MANIFEST.MF</code> (The solution that BalusC posted) when using <code>jar-with-dependencies</code>.
  The problem was that the line appeared in the regular jar's MANIFEST file, but not the jar-with-dependencies.jar's MANIFEST file (2 jar files are generated).
  For anyone who cares how i did it:</p>
  
  <pre><code>&lt;plugin&gt;
      &lt;artifactId&gt;maven-assembly-plugin&lt;/artifactId&gt;
      &lt;version&gt;2.2-beta-5&lt;/version&gt;
      &lt;configuration&gt;
        &lt;archive&gt;
          &lt;manifest&gt;
            &lt;mainClass&gt;${mainClass}&lt;/mainClass&gt;
            &lt;addClasspath&gt;true&lt;/addClasspath&gt;
            &lt;!--at first, i tried to place the Class-Path entry
                right here using &lt;manifestEntries&gt;. see below --&gt;
          &lt;/manifest&gt;
        &lt;/archive&gt;
        &lt;descriptorRefs&gt;
          &lt;descriptorRef&gt;jar-with-dependencies&lt;/descriptorRef&gt;
        &lt;/descriptorRefs&gt;
      &lt;/configuration&gt;
      &lt;executions&gt;
        &lt;execution&gt;
          &lt;goals&gt;
            &lt;goal&gt;attached&lt;/goal&gt;
          &lt;/goals&gt;
          &lt;phase&gt;package&lt;/phase&gt;
          &lt;configuration&gt;
            &lt;descriptorRefs&gt;
              &lt;descriptorRef&gt;jar-with-dependencies&lt;/descriptorRef&gt;
            &lt;/descriptorRefs&gt;
            &lt;archive&gt;
              &lt;manifest&gt;
                &lt;mainClass&gt;${mainClass}&lt;/mainClass&gt;
              &lt;/manifest&gt;
              &lt;!--this is the correct placement --&gt;
              &lt;manifestEntries&gt;
                &lt;Class-Path&gt;.&lt;/Class-Path&gt;
              &lt;/manifestEntries&gt;
            &lt;/archive&gt;
          &lt;/configuration&gt;
        &lt;/execution&gt;
      &lt;/executions&gt;
    &lt;/plugin&gt;
  </code></pre>