How to List Entries in Jar Files using Java Program

The JarFile class is used to read the contents of a JAR file.

Class JarFiles extends the class java.util.zip.ZipFile with support for reading an optional Manifest entry. The Manifest can be used to specify meta-information about the JAR file and its entries.

JarFiles has method called entries() which is used to returns an enumeration of the ZIP file entries. Using Enumeration, You can iterate over entries to list enteries in JarFiles


Java Program to List enteries of Jar Files :

package com.anuj.utils;

import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.jar.JarFile;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Anuj Patel
 */
public class JarOperations {

    public static void main(String[] args) {
        String fileName = "CoreJava.jar";
        File f = new File(fileName);
        if (!fileName.endsWith(".jar")) {
            System.out.println("Invalid FileName");
            System.exit(0);
        }
        
        try {
            JarFile jarFile = new JarFile(fileName);
            Enumeration enumeration = jarFile.entries();
            while(enumeration.hasMoreElements()){
                System.out.println(enumeration.nextElement());                
            }
        } catch (IOException ex) {
     Logger.getLogger(JarOperations.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
}
Output :
META-INF/
META-INF/MANIFEST.MF
com/
com/anuj/
com/anuj/algorithms/
com/anuj/basic/
com/anuj/io/
com/anuj/network/
com/anuj/rmi/
com/anuj/threading/
com/anuj/udp/
com/anuj/utils/
com/anuj/algorithms/ArmStrong.class
com/anuj/algorithms/BubbleSort.class
com/anuj/algorithms/Palindrom.class
com/anuj/basic/ArrayOperations.class
com/anuj/basic/CollectionsUsage.class
com/anuj/basic/LinkedListOperation.class
com/anuj/basic/QueueOperation.class
com/anuj/basic/ReflectionOperations.class
com/anuj/basic/StackOperation.class
com/anuj/basic/TimeOperation.class
com/anuj/io/FileReader.class
com/anuj/io/FileWriter.class
com/anuj/network/ClientSocketInformation.class
com/anuj/network/ParseURL.class
com/anuj/network/RetrieveHTTPHeaderInfo.class
com/anuj/network/RetrieveIPConfigInfo.class
com/anuj/network/RetrieveMachineIPInfo.class
com/anuj/network/RetrieveMyMachineIPInfo.class
com/anuj/network/URLExists.class
com/anuj/rmi/ReceiveMessageInterface.class
com/anuj/rmi/RmiClient.class
com/anuj/rmi/RmiServer.class
com/anuj/threading/CurrentThread.class
com/anuj/threading/MyRunnable.class
com/anuj/threading/MyThread.class
com/anuj/udp/UDPClient$1.class
com/anuj/udp/UDPClient$SendRequest.class
com/anuj/udp/UDPClient.class
com/anuj/udp/UDPServer$1.class
com/anuj/udp/UDPServer$2.class
com/anuj/udp/UDPServer$StartThread.class
com/anuj/udp/UDPServer.class
com/anuj/utils/CopyFile.class
com/anuj/utils/GettingImageFromURL$1.class
com/anuj/utils/GettingImageFromURL.class
com/anuj/utils/ImagePanel.class
com/anuj/utils/LocalesOperations.class
com/anuj/utils/RendomaccessFile.class
com/anuj/utils/RetrieveDaysInMonth.class
com/anuj/utils/RetrieveNetworkInterfaces.class
com/anuj/utils/SystemProperties.class
com/anuj/utils/TestParser.class
com/anuj/utils/TimeZones.class
BUILD SUCCESSFUL (total time: 0 seconds)

No comments:

Post a Comment