Solved

Creating a jar using java api

Posted on 2002-07-21
5
456 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.

631 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