NoSuchFieldError Java

后端 未结 7 1075
盖世英雄少女心
盖世英雄少女心 2020-11-29 03:44

I am getting a NoSuchFieldError in my code, now oracle isn\'t very clear about why this error is thrown only saying: this error can only occur at run time if the definit

7条回答
  •  眼角桃花
    2020-11-29 04:41

    Something to be careful of when tracing these errors in an IDE (Eclipse in my case) is to watch the dependencies of projects your project may depend on. If you use different versions of a library in different dependent projects, the classpath loader may pick up the wrong one. This includes having a project dependent on a jar created from an Eclipse project, and having another project dependent on that project and the project which the jar was generated from. The outdated classes in the jar could potentially be loaded instead of the the classes from the project.

    Example:

    project1 depends on project2 and project3

    project3 depends on project2.jar, a jar generated from the class files in project2

    A final static field is added to a class in project2, which is recompiled, while project2.jar is not rebuilt

    Running project1 may cause the exception, as the classes from project2 may be loaded from the project directly OR the jar, which doesn't have the field

提交回复
热议问题