Go Premium for a chance to win a PS4. Enter to Win


Creating a jar using java api

Posted on 2002-07-21
Medium Priority
Last Modified: 2013-11-23

I have a directory called "temp", which has many subdirectories and files under it. I need to jar temp and everything under it. How do I do it?

I could add a file to the jar, but don't know how to add a directory to it. Following is what I have to add a file to jar:


import java.util.jar.JarOutputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.io.IOException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.*;

public class jartest
    public static void main (String args[]) {
        try {
            String file = "E:\\wrkdir\\practice\\nice.txt";
           BufferedInputStream in = new BufferedInputStream (new FileInputStream (file));
            JarOutputStream jo = new JarOutputStream (new FileOutputStream(new File ("E:\\wrkdir\\practice\\test.jar")));
            jo.putNextEntry (new ZipEntry (file));
            byte[] buffer = new byte [4096];
            int count;
            //copying file source to destination
            while ((count = in.read(buffer)) != -1) {
                jo.write (buffer, 0, count);
            //close the streams
            jo.close ();
        } catch (java.util.zip.ZipException ze) {
            ze.printStackTrace ();
        } catch (java.io.FileNotFoundException fne) {
            fne.printStackTrace ();
        } catch (java.io.IOException ioe) {
            ioe.printStackTrace ();


Any help will be very much appreciated.


Question by:rahman

Accepted Solution

nir2002 earned 600 total points
ID: 7168810

The idea is to get a list of all the files under "temp"
and after use your code with little modification to enter the file into the jar.

1) get the files objects using File API:
a) isDirectory() will let you know if the file is directory
b) listFiles() if the current file object is directory this method return all the files in this directory.
Using them you will creat list of files that are not directories.

2) change your code that get a list of files an foe each file enter it to the jar.

Hope it help
Best regards

Expert Comment

ID: 7168931
Think you are doing a long way...Normally I would not write a class to do it. You could have use the jar command in the command prompt...for best practice, I suggest you put your classes into packages before you jar them. If you need more information on how to jar using command prompt do let me know cos I not sure if I answering your question.

Expert Comment

ID: 7169584
Do you need to jar the directory from an application? Or do you just want to jar the files?
If you want to jar the files, you can use the jar command that comes with the jdk.

Expert Comment

by:Igor Bazarny
ID: 7169875

Jar itself is written in java, classes hidden in tools.jar. You may try something like:

package ee.jar;

import sun.tools.jar.Main;

public class JarTest{
    public static void main(String[] args){
        Main main = new Main(System.out,System.err,"NiceName");
        main.run(new String[]{"cvf", args[0], args[1]});

Igor Bazarny,
Brainbench MVP for Java 1

Expert Comment

ID: 7179739
Like nir2002 said, just make sure that your ZipEntry contains the "Directory name".

So if you have /temp/01/File1.txt
your ZipEntry needs to contain the full path (/temp/01/File1.txt) and not just File1.txt

Hope this helps

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question