Display list of classes inside jar file Using Java

 Posted On  | Yashwant Chavan 

This Java tutorial will help to display list of classes inside jar file. We will read respected jar file using ZipInputStream and display the file details using ZipEntry object.

This utility class will help you to display the list of all .class file names inside jar file. ZipInputStream implements an input stream filter for reading files in the ZIP file format. As you know that JAR file is nothing but a ZIP file with an (optional) manifest file. Includes support for both compressed and uncompressed entries.

Once you get the object of ZipEntry which represent a ZIP file entry. You can able to get the file details like file name , file size and modification time etc

ClassListFromJar.java

package com.technicalkeeda.app;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class ClassListFromJar {
    public static void main(String[] args) throws Exception {
        FileInputStream fileInputStream = new FileInputStream("C:\\article\\commons-logging-1.1.1.jar");
        ZipInputStream jarFileInputStream = new ZipInputStream(fileInputStream);

        List < String > fileNames = new ArrayList < String > ();
        ZipEntry zipEntry = null;

        if (jarFileInputStream != null) {
            while ((zipEntry = jarFileInputStream.getNextEntry()) != null) {

                if (!zipEntry.isDirectory() && zipEntry.getName().endsWith(".class")) {
                    String className = zipEntry.getName().replace('/', '.');
                    className = className.substring(0, className.length() - ".class".length());
                    fileNames.add(className);
                }

            }
        }

        if (fileNames != null && !fileNames.isEmpty()) {
            for (String fileName: fileNames) {
                System.out.println("File Name:- " + fileName);
            }
        }
    }
}

Output

File Name:-org.apache.commons.logging.impl.AvalonLogger
File Name:-org.apache.commons.logging.impl.Jdk13LumberjackLogger
File Name:-org.apache.commons.logging.impl.Jdk14Logger
File Name:-org.apache.commons.logging.impl.Log4JLogger
File Name:-org.apache.commons.logging.impl.LogFactoryImpl$1
File Name:-org.apache.commons.logging.impl.LogFactoryImpl$2
File Name:-org.apache.commons.logging.impl.LogFactoryImpl$3
File Name:-org.apache.commons.logging.impl.LogFactoryImpl
File Name:-org.apache.commons.logging.impl.LogKitLogger
File Name:-org.apache.commons.logging.impl.NoOpLog
File Name:-org.apache.commons.logging.impl.ServletContextCleaner
File Name:-org.apache.commons.logging.impl.SimpleLog$1
File Name:-org.apache.commons.logging.impl.SimpleLog
File Name:-org.apache.commons.logging.impl.WeakHashtable$1
File Name:-org.apache.commons.logging.impl.WeakHashtable$Entry
File Name:-org.apache.commons.logging.impl.WeakHashtable$Referenced
File Name:-org.apache.commons.logging.impl.WeakHashtable$WeakKey
File Name:-org.apache.commons.logging.impl.WeakHashtable
File Name:-org.apache.commons.logging.Log
File Name:-org.apache.commons.logging.LogConfigurationException
File Name:-org.apache.commons.logging.LogFactory$1
File Name:-org.apache.commons.logging.LogFactory$2
File Name:-org.apache.commons.logging.LogFactory$3
File Name:-org.apache.commons.logging.LogFactory$4
File Name:-org.apache.commons.logging.LogFactory$5
File Name:-org.apache.commons.logging.LogFactory$6
File Name:-org.apache.commons.logging.LogFactory
File Name:-org.apache.commons.logging.LogSource


© technicalkeeda.com 2017

 |  Find us on Google+ |  Rss Feed

Loaded in 0.0309 seconds.