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?...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| “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;> |
| |||
| 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); } } } |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| exe file for java applications? | saravanan | Java Programming | 4 | 11-17-2008 01:16 AM |
| play mp3 file using java application? | saravanan | Java Programming | 1 | 03-26-2008 02:15 AM |
| convert Excel file to CSV(or text) file using java | mobilegeek | Java Programming | 2 | 09-06-2007 06:42 AM |
| How to watch a file whenever we change or modify the content using c#? | mobilegeek | C# Programming | 5 | 08-20-2007 07:38 AM |
| How to create a ZIP File in Java? | bluesky | Java Programming | 1 | 07-25-2007 06:03 AM |