Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List of resources in a folder of jar file?

As usually I read resources from jar file as following:

getClassLoader().getResource(pTextPath + "/" + pLang +".xml");

I need to read all resources with certain name from known folder in jar file. E.g. read *.xml from

addon/resources/texts

Could I somehow get from jar files list of resources according to path and name template?

UPDATE: Exact duplication of Get a list of resources from classpath directory Please close the question.

like image 439
FoxyBOA Avatar asked Oct 31 '11 12:10

FoxyBOA


People also ask

Is resources folder included in jar?

When you build a java project and pack it into a jar (or a war), the files under the resources folder are included into the jar.

How can I get a resource folder from inside my JAR file?

What you could do is to use getResourceAsStream() method with the directory path, and the input Stream will have all the files name from that dir. After that you can concat the dir path with each file name and call getResourceAsStream for each file in a loop.

What is the contents of a JAR file?

A Java Archive, or JAR file, contains all of the various components that make up a self-contained, executable Java application, deployable Java applet or, most commonly, a Java library to which any Java Runtime Environment can link. There are two key benefits of using a JAR file.


1 Answers

CodeSource src = MyClass.class.getProtectionDomain().getCodeSource();
if (src != null) {
  URL jar = src.getLocation();
ZipInputStream zip = new ZipInputStream(jar.openStream());
/* Now examine the ZIP file entries to find those you care about. */
 ...
} 
else {
   /* Fail... */
}
like image 120
vikas27 Avatar answered Oct 07 '22 22:10

vikas27