Reading a directory inside a JAR using an InputStreamReader

So this question was asked a million times, I believed, and I read them for several hours and tried several options given by some people, but none of them work for me.

I want to list all the files inside the directory inside the application JAR, so in the IDE this works:

File f = new File(this.getClass().getResource("/resources/").getPath()); for(String s : f.list){ System.out.println(s); } 

This gives me all the files inside the directory.

Now I also tried this:

 InputStream in = this.getClass().getClassLoader().getResourceAsStream("resources/"); InputStreamReader inReader = new InputStreamReader(in); Scanner scan = new Scanner(inReader); while (scan.hasNext()) { String s = scan.next(); System.out.println("read: " + s); } System.out.println("END OF LINE"); 

And from the IDE, it prints ALL the files in the directory. Outside of IDE Prints: "END OF A LINE".

Now I can also find the entry inside the Jar:

  String s = new File(this.getClass().getResource("").getPath()).getParent().replaceAll("(!|file:\\\\)", ""); JarFile jar = new JarFile(s); JarEntry entry = jar.getJarEntry("resources"); if (entry != null){ System.out.println("EXISTS"); System.out.println(entry.getSize()); } 

This is the terrible coding I had to do with this String.

Anyway ... I cannot get the list of resources inside the resource directory in the Jar ... How can I do this ???

+3
java file jar resources
source share
1 answer

There is no way to simply get a filtered list of internal resources without first listing the contents of the Jar file.

Fortunately, this is actually not that difficult (and, fortunately, you did most of the hard work for me).

Basically, if you have a link to a JarFile , you just need to query its entries and JarFile over this list.

By checking the JarEntry name for the required match (i.e. resources ), you can filter out the necessary elements ...

For example...

 import java.io.File; import java.io.IOException; import java.util.Enumeration; import java.util.jar.JarEntry; import java.util.jar.JarFile; public class ReadMyResources { public static void main(String[] args) { new ReadMyResources(); } public ReadMyResources() { JarFile jf = null; try { String s = new File(this.getClass().getResource("").getPath()).getParent().replaceAll("(!|file:\\\\)", ""); jf = new JarFile(s); Enumeration<JarEntry> entries = jf.entries(); while (entries.hasMoreElements()) { JarEntry je = entries.nextElement(); if (je.getName().startsWith("resources")) { System.out.println(je.getName()); } } } catch (IOException ex) { ex.printStackTrace(); } finally { try { jf.close(); } catch (Exception e) { } } } } 

Caveat

This question is actually being asked. Instead, trying to read the contents of the Jar at runtime, it would be better to create some kind of text file containing a list of available resources.

This may be caused by the dynamic build process before creating the Jar file. It would be a much simpler solution to then read this file (e.g. via getClass().getResource() ) and then view the list of resources in a text file ... IMHO

+7
source share

All Articles