1. Overview
1.概述
In this article, we’ll demonstrate the reason behind NoSuchFieldError and discover how to resolve it.
在这篇文章中,我们将展示NoSuchFieldError背后的原因,并发现如何解决它。
2. NoSuchFieldError
2.NoSuchFieldError
As the name suggests, NoSuchFieldError occurs when a specified field doesn’t exist. NoSuchFieldError extends the IncompatibleClassChangeError class and is thrown when the application tries to access or modify a field of an object or a static field of a class but the object or class no longer has that field.
顾名思义,NoSuchFieldError在指定字段不存在时发生。NoSuchFieldError扩展了IncompatibleClassChangeError类,当应用程序试图访问或修改一个对象的字段或一个类的静态字段,但该对象或类不再拥有该字段时,会抛出。
IncompatibleClassChangeError class extends the LinkageError class and occurs when we perform incompatible class definition changes. Finally, LinkageError extends Error and shows that a class has some dependency on another incompatibly changed class.
IncompatibleClassChangeError类扩展了LinkageError类,在我们进行不兼容的类定义更改时发生。最后,LinkageError扩展了Error,并显示一个类对另一个不兼容更改的类有某种依赖性。
Let’s see this error in action with the help of an example. As a first step, let’s create a Dependency class:
让我们在一个例子的帮助下看看这个错误的作用。作为第一步,让我们创建一个Dependency 类。
public class Dependency {
public static String message = "Hello Baeldung!!";
}
Then we’ll create a FieldErrorExample class that refers to a field of our Dependency class:
然后我们将创建一个FieldErrorExample类,引用我们的Dependency类的一个字段。
public class FieldErrorExample {
public static String getDependentMessage() {
return Dependency.message;
}
}
Let’s also add code to check whether we’re getting a message from the Dependency class:
让我们也添加代码来检查我们是否从Dependency类中获得message。
public static void fetchAndPrint() {
System.out.println(getDependentMessage());
}
Now, we can compile these files using the javac command, and upon execution of the FieldErrorExample class using the java command, it will print the specified message.
现在,我们可以使用javac命令编译这些文件,当使用java命令执行FieldErrorExample类时,它将打印指定的message。
However, if we comment out, remove, or change the attribute name in the Dependency class and recompile it, then we’ll run into our error.
然而,如果我们在Dependency类中注释、删除或改变属性名称,并重新编译,那么我们就会遇到我们的错误。
For example, let’s change the attribute name in our Dependency class:
例如,让我们改变我们的Dependency类中的属性名称。
public class Dependency {
public static String msg = "Hello Baeldung!!";
}
Now, if we recompile only our Dependency class, and then execute FieldErrorExample again, we’ll encounter the NoSuchFieldError:
现在,如果我们只重新编译我们的Dependency类,,然后再次执行FieldErrorExample,我们将遇到NoSuchFieldError。
Exception in thread "main" java.lang.NoSuchFieldError: message
The above error occurred because the FieldErrorExample class still refers to the static field message of the Dependency class, but it no longer exists — we’ve made an incompatible change to the Dependency class.
出现上述错误是因为FieldErrorExample类仍然引用Dependency类的静态字段message,但它已经不存在了–我们对Dependency类做了不兼容的修改。
3. Resolving the Error
3.解决错误
To avoid this error, we need to clean and compile the existing files. We can do this using the javac command or with Maven by running mvn clean install. By performing this step, we’ll have all the latest compiled files, and we’ll avoid running into the error.
为了避免这个错误,我们需要清理和编译现有文件。我们可以通过使用javac命令或使用Maven运行mvn clean install.来完成这一步骤,我们将获得所有最新的编译文件,并避免出现错误。
If the error persists, then the problem might be multiple JAR files: one while compiling, and another while running. This often happens when the application depends on external JARs. Here, we should validate the order of the JARs in the build path to identify the inconsistent JAR.
如果错误持续存在,那么问题可能是多个JAR文件:一个在编译时,另一个在运行时。这种情况经常发生在应用程序依赖外部JARs的时候。在这里,我们应该验证构建路径中JAR的顺序,以确定不一致的JAR。
If we have to investigate further, it’s helpful to run the application with -verbose: class option to check the loaded classes. This can help us identify the outdated class.
如果我们必须进一步调查,用-verbose: class选项运行应用程序来检查加载的类是有帮助的。这可以帮助我们识别过时的类。
Sometimes a third-party JAR might be internally referring to another version, which results in NoSuchFieldError. If this happens, we can use mvn dependency:tree -Dverbose. This generates the maven dependency tree and helps us in identifying the inconsistent JAR.
有时,第三方JAR可能在内部引用了另一个版本,这导致了NoSuchFieldError。如果发生这种情况,我们可以使用mvn dependency:tree -Dverbose.,这样可以生成maven依赖树,帮助我们识别不一致的JAR。
4. Conclusion
4.总结
In this short tutorial, we have shown why NoSuchFieldError occurs and looked at how we can resolve it.
在这个简短的教程中,我们展示了为什么会发生NoSuchFieldError,并研究了如何解决它。
As always, the code is available over on GitHub.
像往常一样,代码可在GitHub上获得。