title: java - How to list directories in resources in jar
body: <p>Let's have a maven project with resources in following structure:</p>
  
  <p>src/main/resources</p>
  
  <ul>
  <li>dir1
  
  <ul>
  <li>subdir1
  
  <ul>
  <li>files...</li>
  </ul></li>
  <li>subdir2
  
  <ul>
  <li>files...</li>
  </ul></li>
  </ul></li>
  <li>dir2</li>
  </ul>
  
  <p>Although I found many answers how to list file resources, I am stuck to find a way how to list all direct subdirectories of a directory (or path) in resources, that would work when app is run both from IDE (IntelliJ) and jar.</p>
  
  <p>The goal is to get names of subdirectories of directory "dir1":
  subdir1, subdir2</p>
  
  <p>I tried</p>
  
  <pre><code>Thread.currentThread().getContextClassLoader().getResourceAsStream("dir1");
  </code></pre>
  
  <p>and to use returned InputStream (containing names of subdirectories), but does not work from jar. Works from IntelliJ.</p>
  
  <p>I also tried to use spring framework but</p>
  
  <pre><code>PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
  Resource[] resources = resolver.getResources("dir1/*");
  </code></pre>
  
  <p>does not work as well from jar - <code>resources</code> array is empty. Works from IntelliJ.</p>
  
  <p>I tried to modify to</p>
  
  <pre><code>Resource[] resources = resolver.getResources("dir1/*/");
  </code></pre>
  
  <p>and it works from jar, but does not work from IntelliJ.</p>
  
  <p>When I make it work from jar, I break the other way and vice versa. My last idea is to use</p>
  
  <pre><code>Resource[] resources = resolver.getResources("dir1/**");
  </code></pre>
  
  <p>to get all resources under the directory and then get only ones that are required. Is there a better (preferably not hacky) way?</p>