这来自一个真实世界的例子,一些Hibernate类是一个Antlr类的子类,并试图填充一些字段,这只是在最近的Antlr版本中引入的。
HibernateClass ---subclasses---> AntlrClass
HibernateClass uses AntlrClass.fieldX
如果某人在运行时部署了较旧的Antlr库,则此超类字段将不存在,并且将抛出NoSuchFieldError
。
确保您在运行时动态绑定的超类是兼容版本的标准Java / JVM方法是什么?
答案 0 :(得分:1)
检查出来 -
package com.mobilous.mail;
import java.io.IOException;
import java.util.Map;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
Map<String,Attributes> map = null;
try {
JarFile file = new JarFile("Your library file path");
Manifest manifest = file.getManifest();
System.out.println(manifest.getMainAttributes().getValue("Specification-Version"));
System.out.println(manifest.getMainAttributes().getValue("Implementation-Version"));
map = manifest.getEntries();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
现在您应首先检查您需要检查的每个jar文件的文件版本。如果发现较旧只显示错误消息。
答案 1 :(得分:0)
我接下来会给出自己的答案 -
Java并非旨在在运行时确定编译时超类是否与运行时超类兼容。由于Java使用单独的类路径进行构建和运行,并且由于子类不会从超类中复制内容,因此需要NoSuchFieldErrors。
Java无法解决这个问题。