title: How can I retrieve the directory containing the executed jar file?
body: <p>I am working on a Spring Boot application runned as executable jar. In my code I am trying to retrieve the folder where is located the runned jar file. But I am finding some difficulties. I have done in this way:</p>
  <pre><code>File jarFile = new File(BeanConfig.class.getProtectionDomain().getCodeSource().getLocation().getPath());
  String jarFolder = jarFile.getParentFile().getPath();
  System.out.println(&quot;FOLDER: &quot; + jarFolder);
  </code></pre>
  <p>The problem is that, doing in this way, the printed output is something like this:</p>
  <pre><code>FOLDER: file:/home/andrea/git/notartel-api-batch/target/UpdateInfoBatch-0.0.1-SNAPSHOT.jar!/BOOT-INF
  </code></pre>
  <p>And this is not what I need because I need the path of the directory containing my jar file that int this case is this one: <strong>/home/andrea/git/notartel-api-batch/target/</strong></p>
  <p>What is wrong? What am I missing? How can I try to fix this behavior in order to retrieve the path of the directory containing my jar file?</p>