I am trying to detect which class inside a jar contains main or a supplied method name (if possible).
At the moment I have the following code
public static void getFromJars(String pathToAppJar) throws IOException{
FileInputStream jar = new FileInputStream(pathToAppJar);
ZipInputStream zipSteam = new ZipInputStream(jar);
ZipEntry ze;
while ((ze = zipSteam.getNextEntry()) != null) {
System.out.println(ze.toString());
}
zipSteam.close();
}
This will allow me to get packages and classes under these packages, but I do not know if it is possible to even get methods inside classes. Further, I do not know if this approach is even good for a case of several pkgs inside the jar, since each package can have a class with main in it.
I would appreciate any ideas.
Thanks to fvu's comments, I ended up with the following code.
public static void getFromJars(String pathToAppJar) throws IOException, ClassNotFoundException
{
FileInputStream jar = new FileInputStream(pathToAppJar);
ZipInputStream zipSteam = new ZipInputStream(jar);
ZipEntry ze;
URL[] urls = { new URL("jar:file:" + pathToAppJar+"!/") };
URLClassLoader cl = URLClassLoader.newInstance(urls);
while ((ze = zipSteam.getNextEntry()) != null) {
// Is this a class?
if (ze.getName().endsWith(".class")) {
// Relative path of file into the jar.
String className = ze.getName();
// Complete class name
className = className.replace(".class", "").replace("/", ".");
Class<?> klazz = cl.loadClass(className);
Method[] methodsArray = klazz.getMethods();
}
}
zipSteam.close();
}
I removed the code that uses the methods found, since it is not important for this answer
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With