title: Load .properties file from IDE and also from outside of JAR
body: <p>I have a <code>app.properties</code> file in my maven project under <code>resources</code> folder as shown here (simplified):</p>
  
  <pre>
  myApp
    |----src
    |     |
    |     |--main
    |         |--java
    |         |    |--ApplicationInitializer.java
    |         |
    |         |--resources
    |              |--app.properties
    |
    |---target
          |--myApp.jar
          |--app.properties     
  </pre>
  
  <p>In <code>ApplicationInitializer</code> class I want to load properties from the <code>app.properties</code> file with following piece of code:</p>
  
  <pre><code>Properties props = new Properties();
  
  String path = "/app.properties";
  
  try {
      props.load(ApplicationInitializer.class.getResourceAsStream(path));
  } catch (IOException e) {
      e.printStackTrace();
  }
  
  System.out.println(props.getProperty("property"));
  </code></pre>
  
  <p>This piece of code loads properties correctly when I run it from inside my IDE but fails with exception </p>
  
  <pre><code>Exception in thread "main" java.lang.NullPointerException
      at java.util.Properties$LineReader.readLine(Properties.java:434)
      at java.util.Properties.load0(Properties.java:353)
      at java.util.Properties.load(Properties.java:341)
      at cz.muni.fi.fits.ApplicationInitializer.main(ApplicationInitializer.java:18)
  </code></pre>
  
  <p>when trying to run in as JAR file.</p>
  
  <p>For creating a jar file I am using combination of <b><code>maven-shade-plugin</code></b>, <b><code>maven-jar-plugin</code></b> (for excluding properties file outside of the JAR) and <b><code>maven-resources-plugin</code></b> (for copying properties file to specific folder) in <code>pom.xml</code> file as shown here:</p>
  
  <pre class="lang-xml prettyprint-override"><code>    &lt;plugin&gt;
          &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
          &lt;artifactId&gt;maven-shade-plugin&lt;/artifactId&gt;
          &lt;version&gt;2.3&lt;/version&gt;
          &lt;executions&gt;
              &lt;execution&gt;
                  &lt;phase&gt;package&lt;/phase&gt;
                  &lt;goals&gt;
                      &lt;goal&gt;shade&lt;/goal&gt;
                  &lt;/goals&gt;
                  &lt;configuration&gt;
                      &lt;transformers&gt;
                          &lt;transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"&gt;
                              &lt;mainClass&gt;cz.muni.fi.fits.ApplicationInitializer&lt;/mainClass&gt;
                          &lt;/transformer&gt;
                      &lt;/transformers&gt;
                  &lt;/configuration&gt;
              &lt;/execution&gt;
          &lt;/executions&gt;
      &lt;/plugin&gt;
  
      &lt;plugin&gt;
          &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
          &lt;artifactId&gt;maven-jar-plugin&lt;/artifactId&gt;
          &lt;version&gt;2.5&lt;/version&gt;
          &lt;configuration&gt;
              &lt;excludes&gt;
                  &lt;exclude&gt;**/*.properties&lt;/exclude&gt;
              &lt;/excludes&gt;
          &lt;/configuration&gt;
      &lt;/plugin&gt;
  
      &lt;plugin&gt;
          &lt;artifactId&gt;maven-resources-plugin&lt;/artifactId&gt;
          &lt;version&gt;2.7&lt;/version&gt;
          &lt;executions&gt;
              &lt;execution&gt;
                  &lt;id&gt;copy-resource&lt;/id&gt;
                  &lt;phase&gt;package&lt;/phase&gt;
                  &lt;goals&gt;
                      &lt;goal&gt;copy-resources&lt;/goal&gt;
                  &lt;/goals&gt;
                  &lt;configuration&gt;
                      &lt;outputDirectory&gt;${basedir}/target&lt;/outputDirectory&gt;
                      &lt;resources&gt;
                          &lt;resource&gt;
                              &lt;directory&gt;src/main/resources&lt;/directory&gt;
                          &lt;/resource&gt;
                      &lt;/resources&gt;
                  &lt;/configuration&gt;
              &lt;/execution&gt;
          &lt;/executions&gt;
      &lt;/plugin&gt;
  </code></pre>
  
  <p>I then switched the code in main method to this one:</p>
  
  <pre><code>Properties props = new Properties();
  
  String path = "./app.properties";
  
  try (FileInputStream file = new FileInputStream(path)) {
      props.load(file);
  } catch (IOException e) {
      e.printStackTrace();
  }
  
  System.out.println(props.getProperty("property"));
  </code></pre>
  
  <p>and managed to load properties from file when running JAR but this time I was not able to load them when running in IDE, it ended with the same exception as above.</p>
  
  <p>So my question is: <b>How to set the filepath (or <code>pom.xml</code> file?) that I will be able to load properties running from both IDE and JAR file?</b></p>
  
  <p>Thanks in advance :)</p>