This tutorial will be very interesting. Last week I was working on a Java Project which requires me to have list of Classes from .jar file. With the help of JarEntry and JarInputStream utility I was able to extract all classes from inside of .jar file.
JarInputStream creates a new JarInputStream and reads the optional manifest. If a manifest is present, also attempts to verify the signatures if the JarInputStream is signed.
Below solution will also work for your if you have any of below query:
- In Java – How to find a class somewhere inside dozens of JAR files
- How to get names of classes inside a jar file?
- Looking for a Java class in a set of JARs with find
Java Reflection Tutorial: How To Use Reflection To Call Java Method At Runtime
Let’s get started. We will perform below steps.
- Create class
CrunchifyFindClassesFromJar.java - Create utility method called
getCrunchifyClassNamesFromJar(String jarFileName) - Iterate through nextJarEntry and if found remove .class from name add it to JSON list
- Print JSONObject containing above class list
package com.crunchify.tutorial;
import java.io.FileInputStream;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
import org.json.JSONArray;
import org.json.JSONObject;
/**
* @author Crunchify.com
*
*/
public class CrunchifyFindClassesFromJar {
@SuppressWarnings("resource")
public static JSONObject getCrunchifyClassNamesFromJar(String crunchifyJarName) {
JSONArray listofClasses = new JSONArray();
JSONObject crunchifyObject = new JSONObject();
try {
JarInputStream crunchifyJarFile = new JarInputStream(new FileInputStream(crunchifyJarName));
JarEntry crunchifyJar;
while (true) {
crunchifyJar = crunchifyJarFile.getNextJarEntry();
if (crunchifyJar == null) {
break;
}
if ((crunchifyJar.getName().endsWith(".class"))) {
String className = crunchifyJar.getName().replaceAll("/", "\\.");
String myClass = className.substring(0, className.lastIndexOf('.'));
listofClasses.put(myClass);
}
}
crunchifyObject.put("Jar File Name", crunchifyJarName);
crunchifyObject.put("List of Class", listofClasses);
} catch (Exception e) {
System.out.println("Oops.. Encounter an issue while parsing jar" + e.toString());
}
return crunchifyObject;
}
public static void main(String[] args) {
JSONObject myList = getCrunchifyClassNamesFromJar("/Users/<username>/Documents/javax.servlet.jsp.jar");
System.out.println(myList);
}
}
Result:
{
"List of Class": [
"javax.el.ArrayELResolver",
"javax.el.BeanELResolver$BeanProperties",
"javax.el.BeanELResolver$BeanProperty",
"javax.el.BeanELResolver",
"javax.el.CompositeELResolver$CompositeIterator",
"javax.el.CompositeELResolver",
"javax.el.ELContext",
"javax.el.ELContextEvent",
.....
..... so many more...
],
"Jar File Name": "/Users/<username>/Documents/javax.servlet.jsp.jar"
}
Bonus point:
Check out how to find if particular jar contains specific method.
try {
Class<?> crunchifyClass = Class.forName(className);
Method main = crunchifyClass.getDeclaredMethod("yourMethodName");
main.invoke(null);
} catch (Exception e) {
e.printStackTrace();
}
Hope this mini utility will help if you want to quickly achieve the same goal. Feel free to use it in your production project.


