Though we discussed the basics of class loading in the previous article in this series, we still need more knowledge before we can delve into the advanced class-loading techniques. This article will show how to solve class-loading problems and to overcome some debugging limitations of the JDK class loaders.
Let me issue a warning here that dealing with class loaders is somewhat risky business, because problems seldom arise during the class-loading process itself. Therefore locating a problem is tricky and time-consuming. That said, custom class loaders can also offer many possibilities regular Java classes cannot achieve, and therefore are essential to advanced applications like servers, code-enhancing tools and dynamic modules. In the end, it is up to the architect to decide if the features outweigh the risks and to make sure that debugging and extended tests are provided.
The biggest challenge in dealing with class-loading problems is that problems rarely manifest themselves during the class-loading process but rather during the usage of a class later on. In addition, the messages provided by the JDK are, to say the least, inadequate, and the toString() methods of the JDK class loaders are not overridden to provide more data.
ClassNotFoundExceptionLet´s consider several different class-loading error scenarios. First, we want to figure out what to do when a class could not be found and the application throws a ClassNotFoundException that looks like this:
java.lang.ClassNotFoundException: Class: com/madplanet/article/classloader/
part2/NotToBeFoundClass could not be found
at com.madplanet.article.classloader.
part2.MyClassLoader.findClass
(MyClassLoader.java:41)
at com.madplanet.article.
classloader.part2.MyClassLoader.
loadClass(MyClassLoader.java:56)
at java.lang.ClassLoader.
loadClass(ClassLoader.java:235)
at com.madplanet.test.classloader.
part2.MainTest.testClassNotFound(
MainTest.java:48)
Here are some steps to resolve the issue:
loadClass() call is involved (in our example, the last line).
This problem is caused when a class loader is asked to load a particular class and it cannot be found. Here are few causes:
Java教程分页: 共7页: 上一页 1 [2] [3] [4] [5] [6] [7] 下一页