Skip to content

Instantly share code, notes, and snippets.

@ryanwoodsmall
Created February 22, 2017 18:49
Show Gist options
  • Save ryanwoodsmall/1d259e9753d95367ad79ed3c53cd999f to your computer and use it in GitHub Desktop.
Save ryanwoodsmall/1d259e9753d95367ad79ed3c53cd999f to your computer and use it in GitHub Desktop.
dump .class lists in a .jar file
/*
* via http://stackoverflow.com/questions/15720822/how-to-get-names-of-classes-inside-a-jar-file
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
import java.util.Properties;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class dumpJarClasses {
public static void main(String[] args) throws FileNotFoundException, IOException {
if (args.length <= 0) {
System.out.println("please provide at least one .jar file");
System.exit(1);
}
for (int i = 0; i < args.length; i++) {
String pathParts[] = args[i].split(File.separator);
String fileName = pathParts[pathParts.length - 1];
List<String> classNames = new ArrayList<String>();
ZipInputStream zip = new ZipInputStream(new FileInputStream(args[i]));
for (ZipEntry entry = zip.getNextEntry(); entry != null; entry = zip.getNextEntry()) {
if (!entry.isDirectory() && entry.getName().endsWith(".class")) {
// This ZipEntry represents a class. Now, what class does it represent?
String className = entry.getName().replace('/', '.'); // including ".class"
classNames.add(className.substring(0, className.length() - ".class".length()));
}
}
Iterator<String> classNamesIter = classNames.iterator();
while (classNamesIter.hasNext()) {
//System.out.println(fileName + " : " + classNamesIter.next());
System.out.println(args[i] + " : " + classNamesIter.next());
}
}
System.exit(0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment