类明明存在,为什么加载时抛异常?
书中的例子,程序如下:package com.zhuhe.test;
class Tester
{
static
{
System.out.println("Tester类的静态初始化块");
}
}
public class ClassLoaderTest
{
public static void main(String[] args) throws ClassNotFoundException
{
ClassLoader cl = ClassLoader.getSystemClassLoader();
//下面语句仅仅加载Tester类
cl.loadClass("Tester");
System.out.println("系统加载Tester类");
//下面语句才会初始化Tester类
Class.forName("Tester");
}
}
运行就会抛异常,
Exception in thread "main" java.lang.ClassNotFoundException: Tester
at (URLClassLoader.java:366)
at (URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at (URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at com.zhuhe.test.ClassLoaderTest.main(ClassLoaderTest.java:16)
请教大家,Tester类是存在的,为什么会抛找不到类的异常。谢谢大家!