import java.lang.reflect.*;
import java.util.Collection;
public class ProxyInstan{
public static void main(String[] args) throws
IllegalArgumentException,InstantiationException,IllegalAccessException,InvocationTargetException,SecurityException,NoSuchMethodException{
Class proxy1=Proxy.getProxyClass(Collection.class.getClassLoader(),Collection.class);//获取动态类字节码
//@SuppressWarnings({"unchecked"})
Constructor constructor=proxy1.getConstructor(InvocationHandler.class);//--------------获取相关参数构造函数
class MyInvocationHandler1 implements InvocationHandler{//-------------------------------关于InvocationHandler类型的类
@Override
public Object invoke(Object proxy,Method method,Object[] args)throws Throwable{
return null;
}
}
Collection collection=(Collection) constructor.newInstance(new MyInvocationHandler1());//利用构造函数实例化动态类
Collection collection1=(Collection) constructor
.newInstance(new InvocationHandler() {
@Override
public Object invoke(Object proxy,Method method,Object[] args) throws Throwable{
return null;
}
});
//输出动态类实例对象
System.out.println(collection);
System.out.println(collection1);
}
}
以上程序在Eclipse中能编译运行,有紧告
在命令行下不能运行,说使用了未经检查或不安全的操作,添加@SuppressWarnings({"unchecked"}) 编译通过。但无法加载运行,说找不到或无法加载主类
自学新手请教如何修改?