title: How to get list of resource files in a JAR file
body: <p>I know this question has been asked before but I can't find an answer that appears to be the de-facto way to do it.</p>
  
  <p>I simply want to list all the resource files that are packaged in a particular folder inside my JAR file.</p>
  
  <p>The JAR file contains a folder called variants and inside it are an unknown number of JSON files that I want to list.</p>
  
  <p>Initially I tried this:</p>
  
  <pre><code>    File[] files = new File(getClass().getClassLoader().getResource("variants").getPath()).listFiles();
  </code></pre>
  
  <p>This worked when running the program in Eclipse but not when running the JAR file from the command line because the path returns:</p>
  
  <pre><code>file:/dev-root/target/my-app.jar!/variants
  </code></pre>
  
  <p>Using <a href="https://stackoverflow.com/a/28057735/820657">this</a> answer I was able to get it to work but it seems overly complex, it's not the accepted or even the most popular answer. Is there a straightforward way to achieve what I want?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Using <a href="https://stackoverflow.com/a/21248058/820657">this</a> answer I produced the following:</p>
  
  <pre><code>JarFile jar = new JarFile("target/myapp.jar");
  Enumeration&lt;JarEntry&gt; entries = jar.entries();
  
  while (entries.hasMoreElements())
  {
      JarEntry entry = entries.nextElement();
  
      if (!entry.isDirectory() &amp;&amp; entry.getName().contains("variants"))
      {
          // add name to files []
      }
  }
  </code></pre>
  
  <p>Is it possible to get a list of all the files in one go using this technique?
   E.g. using something like <code>listFiles</code>.</p>