gogoWebsite

NoSuchFieldError Processing

Updated to 1 day ago

Today, the company's project was released and launched, but after it was launched, the product list could not be requested. See the log error as follows.

Caused by: java.lang.NoSuchFieldError: TYPE
  at com.cm.admin.service.goods.GoodsTagService.listAllTagBySpu(GoodsTagService.java:216)
  at com.cm.admin.service.goods.GoodsTagService$$FastClassBySpringCGLIB$$c44fbbaa.invoke(<generated>)
  at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)

Come on, focus on the key points, NoSuchFieldError, locate the specified row, this field is available, and it is normal on both the local and test servers. If this field does not exist, the application should not be started. So what is the reason? Here we first use a demo to reproduce this error.

First, we create two classes, the code is as follows:

public class TestClass {
    public static String str = "Hello By TestClass";
}
public class Example {
    public static void main(String[] args){
        ();
    }
}

Compile and run with the command line, the command is as follows

javac Example.java
java Example

The output is as follows:

Hello By TestClass

Everything is normal, and no NoSuchFieldError appears.

Next, we modify the TestClass class as follows and comment the second line.

public class TestClass {
    //public static String str = "Hello By TestClass";
}

Only compile the TestClass class, not the Example class, and then run the program

javac TestClass.java
java Example

The operation results are as follows:

Exception in thread "main" java.lang.NoSuchFieldError: str
    at Example.main(Example.java:3)

OK, NoSuchFieldError appears.

To handle this error, we need to clear all.classRecompile the files to ensure that all files are up to date.
If this error still exists when running, it may be that the dependency referenced at compile time is different from the runtime version. Here we need to check whether each path and version is wrong.
Maven project general executionmvn cleanThat's it.

The project has been successfully launched. In addition to this, the docker image was not the latest (docker deployment). The two errors together, and there were more twists and turns, so I won’t go into details here.

Reference article

  • - How to solve SuchFieldError | Examples Java Code Geeks - 2018

  • NoSuchFieldError (Java Platform SE 7 )