Detect the main part in the bank using java code.

I am trying to determine which class inside the jar contains the main or provided method name (if possible).

I currently 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 don't know if it is even possible to get methods inside classes. In addition, I do not know if this approach is suitable for the case of several pkg inside the jar, since each package can have a class with the main one in it.

I would be grateful for any ideas.

+5
source share
1 answer

Thanks to fvu comments, I got 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, as this is not important for this answer

+3
source

Source: https://habr.com/ru/post/1234543/


All Articles