I have a jar file which contains only testcase.class files. I need to parse that and find all the classes defined in that jar file and need to generate one report. I tried to do it with the reflection concept in Java. Some how, when loading a class is fine, but trying to get the declared methods of it, I am getting the following error. (Note class is loaded, I am able to get the name and canonical name of the loaded class but I error at trying to get declared methods).
for (String clazz: classes) {
String className = clazz.replaceAll("/",
".").replaceAll(".class","");
try {
URL url = file.toURI().toURL();
URL[] urls = new URL[]{url};
URLClassLoader cl = new URLClassLoader(urls);
Class cls = cl.loadClass(className);
System.out.println("##############################");
cls.getDeclaredMethods();
//clazzes.add(getClassObject(cls));
//break;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
Exception in thread "main" java.lang.NoClassDefFoundError: javax/mail/Authenticator
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1016)
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
at java.base/java.net.URLClassLoader.defineClass(URLClassLoader.java:555)
at java.base/java.net.URLClassLoader$1.run(URLClassLoader.java:458)
at java.base/java.net.URLClassLoader$1.run(URLClassLoader.java:452)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:451)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:588)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
at com.springboot.example.springBootExample.service.JarParserService.getTypes(JarParserService.java:99)
at com.springboot.example.springBootExample.service.JarParserService.constructPackages(JarParserService.java:86)
at com.springboot.example.springBootExample.service.JarParserService.getClassTypes(JarParserService.java:78)
at com.springboot.example.springBootExample.service.JarParserService.scanJar(JarParserService.java:61)
at com.springboot.example.springBootExample.service.JarParserService.getAllPackages(JarParserService.java:34)
at com.springboot.example.springBootExample.service.FindClassesFromJarService.parseJar(FindClassesFromJarService.java:61)
at com.springboot.example.springBootExample.service.FindClassesFromJarService.main(FindClassesFromJarService.java:31)
Caused by: java.lang.ClassNotFoundException: javax.mail.Authenticator
at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:476)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:588)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 17 more
Process finished with exit code 1
Aucun commentaire:
Enregistrer un commentaire