Code for listing classes present in a Jar file


    
package com.wordpress.narendrakadali.util;

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

public class ListAllClassesInJar
{

	/**
	 * @param args
	 * @throws IOException
	 */
	@SuppressWarnings("rawtypes")
	public static void main(String[] args) throws IOException
	{
		List classNames = new ArrayList();
		if (args.length > 0)
		{
			JarFile jarFile = new JarFile(args[0]);
			Enumeration allEntries = jarFile.entries();
			while (allEntries.hasMoreElements())
			{
				JarEntry entry = (JarEntry) allEntries.nextElement();
				String name = entry.getName();
				System.out.println(name);
				if (name.endsWith(".class"))
					classNames.add(name.replace(".class", "").replace("/", "."));
			}

			System.out.println(classNames);
		}
		else
		{
			System.out.println("Please enter the jar filename as argument");
		}

	}

}
Advertisements
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: