java.lang.ClassNotFoundException: com.example.Utils
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at com.example.Main.main(Main.java:10)
´Ë´¦ÏÔʾ`Main.java`µÚ10Ðг¢ÊÔ¼ÓÔØ`com.example.Utils`Ààʧ°Ü¡£
// ×Ô¶¨ÒåÀà¼ÓÔØÆ÷¼ÓÔØ²å¼þÀà
File pluginDir = new File("plugins");
URL[] urls = new URL[pluginDir.listFiles().length];
for (int i = 0; i implClass = pluginLoader.loadClass("com.plugin.ServiceImpl");
Service service = (Service) Proxy.newProxyInstance(
pluginLoader,
new Class[]{Service.class},
new InvocationHandler() {...}
);
Áù¡¢½ø½×µ÷ÊÔ¼¼ÇÉ
6.1 ʹÓÃ`-verbose:class`²ÎÊý
Æô¶¯JVMʱÌí¼Ó¸Ã²ÎÊý¿ÉÊä³öÀà¼ÓÔØÏêÇ飺
java -verbose:class com.example.Main
# Êä³öʾÀý£º
[Loaded com.example.Main from file:/C:/project/target/classes/]
[Loaded java.lang.Object from shared objects file]
6.2 Ô¶³Ìµ÷ÊÔÀà¼ÓÔØ
public class ClassLoaderTree {
public static void printTree(ClassLoader loader, int level) {
for (int i = 0; i
¹Ø¼ü´Ê£ºClassNotFoundException¡¢Àà·¾¶¡¢¶¯Ì¬¼ÓÔØ¡¢Ä£¿éϵͳ¡¢ÒÀÀµ¹ÜÀí¡¢Àà¼ÓÔØÆ÷¡¢JavaÒì³£´¦Àí¡¢¹¹½¨¹¤¾ß¡¢Õï¶Ï·½·¨¡¢Ô¤·À²ßÂÔ