title: Find Self location of a Jar file
body: <p>Is there any way by which we can find the current location from where the jar is forked, programmatically? Say I am executing a Jar from <code>D:/Jars/myjar.jar</code>, so can I get that path from the code?</p>
  
  <hr>
  
  <p>@AljoshaBre I tried this</p>
  
  <pre><code>try {
          File file = new File(getClass().getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
          System.out.println(file.getAbsolutePath());
      } catch (Exception e1) {
  
          e1.printStackTrace();
      } 
  </code></pre>
  
  <p>Is throwing NPE. :(</p>
  
  <p>But </p>
  
  <pre><code>System.out.println(getClass().getProtectionDomain().getCodeSource().getLocation().getPath());
  </code></pre>
  
  <p>is showing client.jar only. Not the absolute path. It is inside a Jar.</p>