背景
因业务需要,我们某个业务需要从远程拿到一批二进制数据,然后将这批数据转为java对象进行处理 数据是通过jprotobuf 将java对象编码后得到的 本地没有这个编码、解码的规则,需要从远程拿取class文件,然后在根据动态加载这个class文件,再根据这个class文件将数据流转为可用的java对象
碰到的问题
在 执行 ProtobufProxy.create(schoolClass); 的时候报错
代码:
public void protobufProxyTest2() throws Exception {
File file1 = new File("D:\\code\\untitled\\class");
URLClassLoader classloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
Method add = URLClassLoader.class.getDeclaredMethod("addURL", new Class[] { URL.class });
add.setAccessible(true);
add.invoke(classloader, new Object[] { file1.toURI().toURL() });
Class<?> schoolClass = classloader.loadClass("School");
Codec schoolCodec = ProtobufProxy.create(schoolClass);
}
报错:
java.lang.IllegalStateException: Compilation failed. class: School$$JProtoBufClass, diagnostics: [School$$JProtoBufClass.java:18: 警告: Can't initialize javac processor due to (most likely) a class loader problem: java.lang.NoClassDefFoundError: com/sun/tools/javac/processing/JavacProcessingEnvironment
public class School$$JProtoBufClass implements com.baidu.bjf.remoting.protobuf.Codec<School>{
^
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
, School$$JProtoBufClass.java:18: 错误: 找不到符号
public class School$$JProtoBufClass implements com.baidu.bjf.remoting.protobuf.Codec<School>{
^
符号: 类 School, School$$JProtoBufClass.java:21: 错误: 找不到符号
public byte[] encode(School t) throws IOException {
^
符号: 类 School
位置: 类 School$$JProtoBufClass, School$$JProtoBufClass.java:29: 错误: 找不到符号
public School decode(byte[] bb) throws IOException {
^
符号: 类 School
位置: 类 School$$JProtoBufClass, School$$JProtoBufClass.java:34: 错误: 找不到符号
public int size(School t) throws IOException {
^
符号: 类 School
位置: 类 School$$JProtoBufClass, School$$JProtoBufClass.java:61: 错误: 找不到符号
public void doWriteTo(School t, CodedOutputStream output)
^
符号: 类 School
位置: 类 School$$JProtoBufClass, School$$JProtoBufClass.java:90: 错误: 找不到符号
public void writeTo(School t, CodedOutputStream output)
^
符号: 类 School
位置: 类 School$$JProtoBufClass, School$$JProtoBufClass.java:95: 错误: 找不到符号
public School readFrom(CodedInputStream input) throws IOException {
^
符号: 类 School
位置: 类 School$$JProtoBufClass]
at com.baidu.bjf.remoting.protobuf.utils.compiler.JdkCompiler.doCompile(JdkCompiler.java:202)
at com.baidu.bjf.remoting.protobuf.utils.compiler.AbstractCompiler.compile(AbstractCompiler.java:43)
at com.baidu.bjf.remoting.protobuf.ProtobufProxy.doCreate(ProtobufProxy.java:395)
at com.baidu.bjf.remoting.protobuf.ProtobufProxy.create(ProtobufProxy.java:297)
at com.baidu.bjf.remoting.protobuf.ProtobufProxy.create(ProtobufProxy.java:262)
at com.baidu.bjf.remoting.protobuf.ProtobufProxy.create(ProtobufProxy.java:234)
at com.baidu.bjf.remoting.protobuf.ProtobufProxy.create(ProtobufProxy.java:193)
at Test2.protobufProxyTest2(Test2.java:71)
追了一波源码,发现调了JavaCompiler进行编译class文件(可以理解为执行javac命令)
位置: JdkCompiler-146行
解决方案 (不想看debug过程直接点这里)
研究了一番,猜测是因为项目启动时没指定此类所在目录,于是在项目启动时执行下面的方法
addClassPath为下载的class文件所在目录
public static void putClasspath(String addClassPath){
String property = System.getProperty("java.class.path");
System.setProperty("java.class.path", property +";"+addClassPath+";");
System.out.println(System.getProperty("java.class.path"));
}
完美解决!!
测试代码:
@Test
public void protobufProxyTest() throws Exception {
putClasspath("D:\\code\\untitled\\class");
File file1 = new File("D:\\code\\untitled\\class");
URLClassLoader classloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
Method add = URLClassLoader.class.getDeclaredMethod("addURL", new Class[] { URL.class });
add.setAccessible(true);
add.invoke(classloader, new Object[] { file1.toURI().toURL() });
School2 school = new School2();
school.setSchoolLocation("schoolLocation");
school.setExtend(11111111L);
Codec<School2> tCodec = ProtobufProxy.create(School2.class);
byte[] encode = tCodec.encode(school);
School2 encode1 = tCodec.decode(encode);
System.out.println(encode1);
Class<?> schoolClass = classloader.loadClass("School");
Codec schoolCodec = ProtobufProxy.create(schoolClass);
Object decode = schoolCodec.decode(encode);
System.out.println(decode);
}
public static void putClasspath(String addClassPath){
String property = System.getProperty("java.class.path");
System.setProperty("java.class.path", property +";"+addClassPath+";");
System.out.println(System.getProperty("java.class.path"));
}
输出日志 测试demo地址 https://gitee.com/ayezs/protobuf-proxy-test
|