IT Community - Software Programming, Web Development and Technical Support

How to list the content of jar file in java?

This is a discussion on How to list the content of jar file in java? within the Java Programming forums, part of the Software Development category; How to list the content of jar file in java?...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > Java Programming

Register FAQ Members List Calendar Mark Forums Read
  #1  
Old 07-30-2007, 12:06 AM
oxygen oxygen is offline
D-Web Architect
 
Join Date: Jun 2007
Posts: 632
oxygen is on a distinguished road
Question How to list the content of jar file in java?

How to list the content of jar file in java?
__________________
The OXYGEN
Delivers edgy, intelligent Technology to all...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2  
Old 07-30-2007, 12:10 AM
kingmaker kingmaker is offline
D-Web Genius
 
Join Date: Jun 2007
Posts: 881
kingmaker is on a distinguished road
Send a message via MSN to kingmaker
Default Re: How to list the content of jar file in java?

“jar” command line tool is used to create or list contents of a jar file. In a Java program it is
also possible to edit contents of a jar file. In “java.util.jar” package there are five ways to
create a JAR file using JarFile.

JarFile(File file) .
JarFile(File file, boolean verify) .
JarFile(File file, boolean verify, int mode) .
JarFile(String name) .
JarFile(String name, boolean verify) .
The default is to verify the file when “verify” flag is not specified. The mode setting

Is created with the help of two class constants “OPEN_READ” or “OPEN_READ |
OPEN_DELETE”.

If “OPEN_DELETE” is set, the file is flagged for deletion by the system.

There are two key classes of the API.

JarFile.
JarEntry.
The JarFile class provides a reference to the whole JAR file, While the
JarEntry class points to each of the files contained within the JAR.

A Simple Example of using JarFile:

JarFile jar = new JarFile ("myjar.jar");After getting a reference to a JAR file, list of entries
can be obtained with the help of “entries” method. This method returns an Enumeration of
JarEntry objects. For each JarEntry object, Specific information about the entry can be
obtained by API (eg. “getSize () “method). It is important that most of the methods are
inherited from the parent “ZipEntry” class.Run the following program to list contents of a
“Jar” file. It takes name of jar/zip file as command line argument and list contents of jar files.

import java.io.*;

import java.util.*;

import java.util.jar.*;

public class ShowJarContents {

public static void main(String args[]) {

for (int count=0, n=args.length; count<n;>

try {

showJar(args[count]);

} catch (IOException e) {

System.err.println("Errors In reading: " + args[count]);

}

}

}

private static void showJar(String name)

throws IOException {

System.out.println("Jar: " + name);

JarFile jar = new JarFile(name);

Enumeration e = jar.entries();

while (e.hasMoreElements()) {

listInfo((JarEntry)e.nextElement());

}

System.out.println();

}

private static void listInfo(JarEntry entry) {

System.out.println("n" + entry.getName());

}

}</n;>
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3  
Old 09-13-2007, 11:46 PM
leoraja8 leoraja8 is offline
D-Web Sr.Programmer
 
Join Date: May 2007
Posts: 190
leoraja8 is on a distinguished road
Post Re: How to list the content of jar file in java?

following program used to List all file names in a jar file............

import java.io.IOException;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class MainClass {

public static void main(String[] args) throws IOException {

JarFile jf = new JarFile(args[0]);
Enumeration e = jf.entries();
while (e.hasMoreElements()) {
JarEntry je = (JarEntry) e.nextElement();
String name = je.getName();
System.out.println(name);
}
}
}
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
convert Excel file to CSV(or text) file using java mobilegeek Java Programming 3 02-02-2009 12:02 AM
exe file for java applications? saravanan Java Programming 4 11-17-2008 12:16 AM
play mp3 file using java application? saravanan Java Programming 1 03-26-2008 01:15 AM
How to watch a file whenever we change or modify the content using c#? mobilegeek C# Programming 5 08-20-2007 06:38 AM
How to create a ZIP File in Java? bluesky Java Programming 1 07-25-2007 05:03 AM


All times are GMT -7. The time now is 05:52 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.
Our Partners
One Way Moving Companies | Stamford Dentist | Euro Millions Lottery | Home Loans| Furniture

SEO by vBSEO 3.0.0