我正在测试从另一个类访问私有方法的示例并遇到异常
public class WithoutMain
{
public static void main(String args[]) throws Exception
{
Class c = Class.forName("A");
Object o = c.newInstance();
Method m = c.getDeclaredMethod("message", null);
m.setAccessible(true);
m.invoke(o, null);
}
}
public class A {
private void message(){
System.out.println("This is a private method.");
}
}
得到以下异常
Exception in thread "main" java.lang.ClassNotFoundException: A
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
这 2 个类位于同一个包中。谁能告诉我为什么会出现这个异常?