Solved

Creating a jar using java api

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
web application structure 18 135
sql import cannot be resolved jsp 3 117
Java program running SQL query 5 66
Java array sort 10 63
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

738 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