How to extract a folder from JAR

Using the classloader you cannot retrieve a folder as it can not be a resource of your classpath.

Several solutions are possible:

  • Using the classloader getResource method, retrieve all resources of your folder one by one if you know in advance the file names you are looking for.
  • Pack your complete folder into an archive that you can retrieve from the classloader using the previous method.
  • Unzip your jar directly to retrieve the contained folder. It requires to know the precise location of the jar from the filesystem. This is not always possible depending on the application and is not portable.

I would preferably going for the second solution that is more portable and flexible but requires to repack the archive for all modifications of the folder content.


Jar is simple ZIP file. You can use java.util.zip.* package to decompress files.

Tags:

Java