The class outside the java jar package reflects classNotFoundException

First introduce the background:
the environment is a web environment, tomcat7.x, an external sdk (a jar package) needs to be used in the project as a client to access the external server for authentication. The sdk provides a configuration property to specify a class for connecting to the server. This class needs to implement the XXXHelper interface in the package. The package uses a xxxProvider.class to reflect the creation of configuration class objects.
But the package uses class.ForName("package.className"), and ClassNotFoundException is reported every time.
Solution 1 can only be placed in this jar package
Solution 2
Later, decompile and replace the xxxProvider that created the object, and replace the class.ForName with the following.

Thread.currentThread().getContextClassLoader().loadClass($YOUR_CLASS_NAME) In
this case, you need to communicate with the team that provides the sdk to modify it.

By comparing the classLoader used to load the instance and the classLoader of xxxProvider, it is found that WebappClassLoader (Thread.currentThread().getContextClassLoader()) is used to create an instance successfully, while xxxProvider uses StandardClassLoader, and WebappClassLoader is the StandardClassLoader sub-loader. According to the class loading mechanism, StandardClassLoader cannot load this class.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326380011&siteId=291194637