How to write a Java program which can extract a JAR file and store its data in specified directory (location)?

Posted on

Problem :

I have created a JAR file. Now, I created another Java program. I want to unpack that JAR file in some other directory, meaning I want to do something like unzip.

If I run jar -xf filename.jar this causes some error:

Exception in thread "main" Cannot run program "jar": error=2, No such file or directory
     at java.lang.ProcessBuilder.start(
     at java.lang.Runtime.exec(`

Solution :

Adapt this example: How to extract Java resources from JAR and zip archive

Or try this code:

Extract the Contents of ZIP/JAR Files Programmatically

Suppose jarFile is the jar/zip file to be extracted. destDir is the path where it will be extracted:

java.util.jar.JarFile jar = new java.util.jar.JarFile(jarFile);
java.util.Enumeration enumEntries = jar.entries();
while (enumEntries.hasMoreElements()) {
    java.util.jar.JarEntry file = (java.util.jar.JarEntry) enumEntries.nextElement(); f = new + + file.getName());
    if (file.isDirectory()) { // if its a directory, create it
    } is = jar.getInputStream(file); // get the input stream fos = new;
    while (is.available() > 0) {  // write contents of 'is' to 'fos'


You can use this code snippet as a reference to get your task done.Its almost the same as the code snippet shown above by @JuanZe except that for those who were getting the FileNotFoundException, i have added a small code snippet that will check if the file does exist and if it doesn’t then it will create the parent folder along with the files and will extract the contents of jar file inside the specified destination folder.

Code snippet:

public class JarDemo {

  public static void main(String[] args) throws {
    java.util.jar.JarFile jarfile = new java.util.jar.JarFile(new"E:/sqljdbc4.jar")); //jar file path(here sqljdbc4.jar)
    java.util.Enumeration<java.util.jar.JarEntry> enu= jarfile.entries();
        String destdir = "E:/abc/";     //abc is my destination directory
        java.util.jar.JarEntry je = enu.nextElement();

        System.out.println(je.getName()); fl = new, je.getName());
            fl = new, je.getName());
        } is = jarfile.getInputStream(je); fo = new;



JarFile class.

JarFile file = new JarFile("file.jar");   
for (Enumeration<JarEntry> enum = file.entries(); enum.hasMoreElements();) {   
    JarEntry entry =;   

Here is what I would do to extract my whole “resources” folder from my jar. It is way more faster to use BufferedReader and BufferedWriter.

 public static boolean extractResourcesToTempFolder() {
    try {
        //If folder exist, delete it.
        String destPath = getTempDir() + File.separator + "JToolkit" + File.separator;
        deleteDirectoryRecursive(new File(destPath));            

        JarFile jarFile = new JarFile(JToolkit.class.getProtectionDomain().getCodeSource().getLocation().getPath());
        Enumeration<JarEntry> enums = jarFile.entries();
        while (enums.hasMoreElements()) {
            JarEntry entry = enums.nextElement();
            if (entry.getName().startsWith("resources")) {
                File toWrite = new File(destPath + entry.getName());
                if (entry.isDirectory()) {
                InputStream in = new BufferedInputStream(jarFile.getInputStream(entry));
                OutputStream out = new BufferedOutputStream(new FileOutputStream(toWrite));
                byte[] buffer = new byte[2048];
                for (;;) {
                    int nBytes =;
                    if (nBytes <= 0) {
                    out.write(buffer, 0, nBytes);
    } catch (IOException ex) {
        Logger.getLogger(Methods.class.getName()).log(Level.SEVERE, null, ex);
        return false;
    return true;

Old question.
Here is a updated answer using :

  • Java 7 java.nio to create and copy efficiently entries
  • Java 8 stream to sort and collect the entries lexicographically (in order to always create folders first).

Note that I used (the base class) instead of java.util.jar.JarFile (the subclass).
The last one performs more things that are not required to “just” extract files from an archive.
So it reduces the overhead and prevents exception rising related to security concerns. But if required you can of course replace ZipFile/ZipEntry by JarFile/JarEntry.

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Comparator;
import java.util.List;

public class FileUtils {

    public static void extractArchive(Path archiveFile, Path destPath) throws IOException {

        Files.createDirectories(destPath); // create dest path folder(s)

        try (ZipFile archive = new ZipFile(archiveFile.toFile())) {

            // sort entries by name to always create folders first
            List<? extends ZipEntry> entries =

            // copy each entry in the dest path
            for (ZipEntry entry : entries) {
                Path entryDest = destPath.resolve(entry.getName());

                if (entry.isDirectory()) {

                Files.copy(archive.getInputStream(entry), entryDest);


Your title doesn’t seem to match the question very well but if you really do want to “write [a] java program extracting a jar file” you just need Class JarFile.

You can use this very simple library to pack/unpack jar file


Very simple

import java.util.List;

import fr.stevecohen.jarmanager.JarUnpacker;

class Test {
   JarUnpacker jarUnpacker = new JarUnpacker(); 
   File myfile = new File("./myfile.jar");
   File unpackDir = new File("./mydir");

   List<File> unpacked_files = jarUnpacker.unpack(myfile.getAbsolutePath(), unpackDir.getAbsolutePath());

You can also use maven dependency


You also need my repository

    <name>ReaperSoon's repo</name>

Check the last version with the link bellow to use the last dependency

Please use my public issue tracker if you find some bugs

Well here’s my version, using try-with-resources:

    try (JarFile jarFile = new JarFile(artifact.getFile())) {
        for (JarEntry entry : Collections.list(jarFile.entries())) {
            try (InputStream is = jarFile.getInputStream(entry)) {
                File file = new File(targetDir, entry.getName());
                try (FileOutputStream fos = new FileOutputStream(file)) {
    } catch (IOException e) {
        throw new MyException(String.format(
            "Unable to open jar %s", artifact.getArtifactId()), e);

In case anyone is interested..

Here’s a version for Java 7+ using buffered I/O and try-with-resources.

public static void unzip(File archive, File destDir) throws IOException {
    byte[] buffer = new byte[256 * 1024];
    try (JarFile jar = new JarFile(archive)) {
        Enumeration<JarEntry> entries = jar.entries();
        while (entries.hasMoreElements()) {
            JarEntry ent = entries.nextElement();
            File f = new File(destDir, ent.getName());
            if (ent.isDirectory()) {
            try (InputStream is = jar.getInputStream(ent);
                 FileOutputStream os = new FileOutputStream(f)) {
                for (int r; (r = > 0; ) {
                    os.write(buffer, 0, r);

Here’s my version, using Java 8 and
Its work for me.

File userDir = new File("resources");
JarFile jar = new JarFile("123.jar");
   .filter(file -> 
    file.getName().contains("BOOT-INF/classes") && !file.getName().contains("BOOT-INF/classes/ch/"))
   .peek(file -> {
File res = new File(userDir.getPath() + file.getName().replaceAll("BOOT-INF/classes", ""));

     if (file.isDirectory()) {
     } else { try {
                  FileUtils.copyInputStreamToFile(jar.getInputStream(file), res);
            } catch (IOException e) {
     }).forEach(file ->"Extract file: " + file.getName()));

Leave a Reply

Your email address will not be published. Required fields are marked *