ClassNotFoundException exception occurs in Java reflection analysis of specified jar package, how to handle it

In the last blog post, I wrote that the specified jar package was parsed through Java’s reflection mechanism, and the full class name, method name, and parameter name were obtained. However, some bloggers may find some problems when using it. The problem is that when parsing a simple interface case, it can be parsed completely, but if a complex jar package is parsed, a ClassNotFoundException exception occurs. So what is a complex Jar package? The small DOme we usually write can basically be realized by relying on the JDK jar package. However, when we introduce other dependent jar packages into our project, when we use the previously written method to parse through reflection, a ClassNotFoundException will occur. abnormal. In this article we focus on solving this problem.

Let’s demonstrate it with a simple case!

For example: We want to parse the complex jar package: HZM-payment-api-1.1.1021.jar.

We know that the jar packages this jar package depends on are: activation-1.1.jar, aliyun-java-sdk-core-3.0.0.jar, aliyun-java-sdk-dm-3.0.0-rc1.jar, commons-codec -1.6.jar, commons-io-2.1.jar. . . . A series of jar packages.

What we have to do next is to add these jar packages to URLClassLoader to build the running environment, so that no exceptions will occur. The specific code is as follows:

public class jarTest {
\t
public static void getJarName(String jarFile) throws Exception {
\t\t  
try{
//Create a new File instance by converting the given pathname string to an abstract pathname
File f = new File(jarFile);
URL url1 = f.toURI().toURL();
\t            
File f1 = new File("F:\lib\lib\activation-1.1.jar");
URL url2 = f1.toURI().toURL();
\t            \t            
File f2 = new File("F:\lib\lib\aliyun-java-sdk-core-3.0.0.jar");
URL url3 = f2.toURI().toURL();
\t            
File f3 = new File("F:\lib\lib\aliyun-java-sdk-dm-3.0.0-rc1.jar");
URL url4 = f3.toURI().toURL();
\t            \t            
File f4 = new File("F:\lib\lib\commons-codec-1.6.jar");
URL url5 = f4.toURI().toURL();
\t            
File f5 = new File("F:\lib\lib\commons-io-2.1.jar");
URL url6 = f5.toURI().toURL();
\t            
File f6 = new File("F:\lib\lib\commons-lang3-3.1.jar");
URL url7 = f6.toURI().toURL();
\t            
File f7 = new File("F:\lib\lib\commons-logging-1.1.1.jar");
URL url8 = f7.toURI().toURL();
\t            
File f8 = new File("F:\lib\lib\ELP-common-branch-1.95.jar");
URL url9 = f8.toURI().toURL();
\t            
File f9 = new File("F:\lib\lib\fastjson-1.1.43.jar");
URL url10 = f9.toURI().toURL();
\t            
File f10 = new File("F:\lib\lib\httpclient-4.2.1.jar");
URL url11 = f10.toURI().toURL();
\t            
File f11 = new File("F:\lib\lib\httpcore-4.2.1.jar");
URL url12 = f11.toURI().toURL();
\t            
File f12 = new File("F:\lib\lib\jaxrs-api-3.0.7.Final.jar");
URL url13 = f12.toURI().toURL();
\t            
File f13 = new File("F:\lib\lib\jboss-annotations-api_1.1_spec-1.0.1.Final.jar");
URL url14 = f13.toURI().toURL();
\t            
File f14 = new File("F:\lib\lib\jcip-annotations-1.0.jar");
URL url15 = f14.toURI().toURL();
\t            
File f15 = new File("F:\lib\lib\jcl-over-slf4j-1.7.10.jar");
URL url16 = f15.toURI().toURL();
\t            
File f16 = new File("F:\lib\lib\log4j-over-slf4j-1.7.10.jar");
URL url17 = f16.toURI().toURL();
\t            
File f17 = new File("F:\lib\lib\logback-classic-1.1.2.jar");
URL url18 = f17.toURI().toURL();
\t            
File f18 = new File("F:\lib\lib\resteasy-jaxrs-3.0.7.Final.jar");
URL url19 = f18.toURI().toURL();
\t            
File f19 = new File("F:\lib\lib\slf4j-api-1.7.10.jar");
URL url20 = f19.toURI().toURL();
\t            
File f20 = new File("F:\lib\lib\taobao-sdk-java-auto-20160607.jar");
URL url21 = f20.toURI().toURL();
\t            
File f21 = new File("F:\lib\lib\HZM-common-1.0.jar");
URL url22 = f21.toURI().toURL();
\t            
URLClassLoader myClassLoader = new URLClassLoader(new URL[]{url1,url2,url3,url4,
url5,url6,url7,url8,url9,url10,url11,url12,url13,url4,url15,url16,url17,
url18,url19,url20,url21,url22},Thread.currentThread().getContextClassLoader());
\t              
\t            \t            \t            
//Get all classes through jarFile and JarEntry
JarFile jar = new JarFile(jarFile);
System.out.println("The parsed file name is: " + jarFile);
//Returns an enumeration of zip file entries
Enumeration<JarEntry> enumFiles = jar.entries();
JarEntry entry;
\t              
//Test whether this enumeration contains more elements
while(enumFiles.hasMoreElements()){
entry = (JarEntry)enumFiles.nextElement();
if(entry.getName().indexOf("META-INF")<0){
String classFullName = entry.getName();
if(!classFullName.endsWith(".class")){
classFullName = classFullName.substring(0,classFullName.length()-1);
} else{
//Remove suffix .class
String className = classFullName.substring(0,classFullName.length()-6).replace("/", ".");
Class<?> myclass = myClassLoader.loadClass(className);
// Class<?> myclass=myClassLoader.findClass(className);
                           //Determine whether it is an interface and only parse the interface
boolean isInterface =myclass.isInterface();

if(isInterface){
//print class name
System.out.println("****************************");
System.out.println("Full class name:" + className);
System.out.println(isInterface);
\t                        \t                       \t                      \t                        
//Get the attributes contained in the class
Method[] methods = myclass.getMethods();
for (Method method : methods) {
String methodName = method.getName();
System.out.println("Method name:" + methodName);
Class<?>[] parameterTypes = method.getParameterTypes();
for (Class<?> clas : parameterTypes) {
// String parameterName = clas.getName();
String parameterName = clas.getSimpleName();
System.out.println("Parameter type:" + parameterName);
}
System.out.println("==========================");
}
}
}
}
}
} catch(IOException e){
e.printStackTrace();
}
}
\t      
\t      
\t      
\t  
\t  
public static void main(String[] args) throws Exception {
//The path where the jar package is located
\t  
getJarName("F:\lib\lib\HZM-payment-api-1.1.1021.jar");
}
\t
}

Many bloggers may find it troublesome to manually add jar packages. Next I will write an article about uploading jar packages through file upload to avoid tedious manual input.