I need to count the number of compiled classes, interfaces and enums in a given jar file programmatically (so I need three separate numbers). Which API would help me? (I can’t use third party libraries.)
I’ve already tried quite tricky scheme which seems not always correct. Namely, I read each ZipEntry into a byte[] and then feed the result to my custom class loader which extends standard CalssLoader and just sends this byte[] to ClassLoader.defineClass (which is protect and couldn’t be called from application code directly). Full code is on the Pastebin.
A jar file is a zip file with a specific pattern.
You may use a ZipFile and a ZipEntry or their children classes JarFile and JarEntry.
This code (a method of a custom classloader) will return a Map with arrays of each type of “class” you need.