Solved

Creating a jar using java api

Posted on 2002-07-21
5
432 Views
Last Modified: 2013-11-23
Hi,

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
            in.close();
            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.

Thanks!

0
Comment
Question by:rahman
5 Comments
 
LVL 2

Accepted Solution

by:
nir2002 earned 200 total points
ID: 7168810
Hi,

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
Nir
0
 
LVL 1

Expert Comment

by:mErCuR4
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.
0
 
LVL 3

Expert Comment

by:yasser_helmy
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.
0
 
LVL 7

Expert Comment

by:Igor Bazarny
ID: 7169875
Hi,

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]});
    }
}

Regards,
Igor Bazarny,
Brainbench MVP for Java 1
0
 

Expert Comment

by:Fortress_Initiative
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
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

777 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