动态加载的类放在package com.sun.rio.demos.hello包中,如下 package com.sun.rio.demos.hello; public class packing { public Object Run(MyObj o,Object arg1,Object arg2) { return o.Go(arg1,arg2); } }调用c = Class.forName("com.sun.rio.demos.hello.packing"); 出现java.lang.ClassNotFoundException: com.sun.rio.demos.hello.packing
动态加载的测试程序也在com.sun.rio.demos.hello包
我这样用没有问题, package com.sun.rio.demos.hello;import java.lang.reflect.Method;/** * @author Administrator * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates */ public class Test { public static void main(String[] args) { Class c = null; try { c =Class.forName("com.sun.rio.demos.hello.Packing"); } catch (ClassNotFoundException e1) { e1.printStackTrace(); } Object ob = null; try { ob = c.newInstance(); } catch (InstantiationException e2) { e2.printStackTrace(); } catch (IllegalAccessException e2) { e2.printStackTrace(); }
另外Class.forName为什么会抛异常?
package com.sun.rio.demos.hello;
public class packing
{
public Object Run(MyObj o,Object arg1,Object arg2)
{
return o.Go(arg1,arg2);
}
}调用c = Class.forName("com.sun.rio.demos.hello.packing");
出现java.lang.ClassNotFoundException: com.sun.rio.demos.hello.packing
package com.sun.rio.demos.hello;import java.lang.reflect.Method;/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class Test { public static void main(String[] args) {
Class c = null;
try {
c =Class.forName("com.sun.rio.demos.hello.Packing");
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
} Object ob = null;
try {
ob = c.newInstance();
} catch (InstantiationException e2) {
e2.printStackTrace();
} catch (IllegalAccessException e2) {
e2.printStackTrace();
}
Class[] arg3 = {(new MyObj()).getClass(),(new Object()).getClass(),(new Object()).getClass()};
Method m3 = null;
try {
m3 = c.getMethod("Run", arg3);
} catch (SecurityException e3) {
e3.printStackTrace();
} catch (NoSuchMethodException e3) {
e3.printStackTrace();
}
}
}
-------------------------------
package com.sun.rio.demos.hello;public class MyObj
{
public Object Go(Object arg1,Object arg2)
{
return new Object();
}
}
-----------------------------
packing.java
-----------------------------
package com.sun.rio.demos.hello;public class packing
{
public Object Run(MyObj o,Object arg1,Object arg2)
{
return o.Go(arg1,arg2);
}
}
--------------------------
Test.java
----------------
package com.sun.rio.demos.hello;import java.lang.reflect.Method;public class Test {
public static void main(String[] args){
Class c = null;
try {
c = Class.forName("com.sun.rio.demos.hello.packing");
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
} Object ob = null;
try {
ob = c.newInstance();
} catch (InstantiationException e2) {
e2.printStackTrace();
} catch (IllegalAccessException e2) {
e2.printStackTrace();
}
Class[] arg3 = {(new MyObj()).getClass(),(new Object()).getClass(),(new Object()).getClass()};
Method m3 = null;
try {
m3 = c.getMethod("Run", arg3);
} catch (SecurityException e3) {
e3.printStackTrace();
} catch (NoSuchMethodException e3) {
e3.printStackTrace();
} }}