Solved

Creating a jar using java api

Posted on 2002-07-21
5
421 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now