问题描述
今天在学习 JDK 动态代理时发现找不到 ProxyGenerator 类。具体代码如下:
package com.ssm.test;
import com.ssm.dao.IndexDao;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.ProxyGenerator;
public class Test {
public static void main(String[] args) throws IOException {
Class<?>[] interfaces = new Class[]{IndexDao.class};
byte[] bytes = ProxyGenerator.generateProxyClass("SSM",interfaces);
File file = new File("c:\\Test.class");
FileOutputStream fw = new FileOutputStream(file);
fw.write(bytes);
fw.flush();
fw.close();
}
}
错误信息
Error:(8, 25) java: 找不到符号
符号: 类 ProxyGenerator
位置: 程序包 java.lang.reflect
解决方法
将 JDK 版本更换为 JDK8 即可。
然后重新导入 import sun.misc.ProxyGenerator
包下的 ProxyGenerator
类
package com.ssm.test;
import com.ssm.dao.IndexDao;
import sun.misc.ProxyGenerator;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test {
public static void main(String[] args) throws IOException {
Class<?>[] interfaces = new Class[]{IndexDao.class};
byte[] bytes = ProxyGenerator.generateProxyClass("SSM",interfaces);
File file = new File("c:\\Test.class");
FileOutputStream fw = new FileOutputStream(file);
fw.write(bytes);
fw.flush();
fw.close();
}
}
我们发现代码中不再报红了。