How do I fix a NoSuchMethodError?

2018-12-31 01:41发布

I'm getting a NoSuchMethodError error when running my Java program. What's wrong and how do I fix it?

25条回答
低头抚发
2楼-- · 2018-12-31 02:05

If you are writing a webapp, ensure that you don't have conflicting versions of a jar in your container's global library directory and also in your app. You may not necessarily know which jar is being used by the classloader.

e.g.

  • tomcat/common/lib
  • mywebapp/WEB-INF/lib
查看更多
旧时光的记忆
3楼-- · 2018-12-31 02:05

It means the respective method is not present in the class:

  1. If you are using jar then decompile and check if the respective version of jar have proper class.
  2. Check if you have compiled proper class from your source.
查看更多
君临天下
4楼-- · 2018-12-31 02:05

If your file name is different than the class name which contain main method then it may be the possibility that this error may cause.

查看更多
牵手、夕阳
5楼-- · 2018-12-31 02:07

Try this way: remove all .class files under your project directories (and, of course, all subdirectories). Rebuild.

Sometimes mvn clean (if you are using maven) does not clean .class files manually created by javac. And those old files contain old signatures, leading to NoSuchMethodError.

查看更多
低头抚发
6楼-- · 2018-12-31 02:08

I fixed this problem in Eclipse by renaming a Junit test file.
In my Eclipse work space I have an App project and a Test project.
The Test project has the App project as a required project on the build path.

Started getting the NoSuchMethodError.
Then I realized the class in the Test project had the same name as the class in the App project.

App/  
  src/
     com.example/  
       Projection.java
Test/  
  src/
     com.example/
       Projection.java

After renaming the Test to the correct name "ProjectionTest.java" the exception went away.

查看更多
荒废的爱情
7楼-- · 2018-12-31 02:09

These problems are caused by the use of the same object at the same two classes. Objects used does not contain new method has been added that the new object class contains.

ex:

filenotnull=/DayMoreConfig.conf
16-07-2015 05:02:10:ussdgw-1: Open TCP/IP connection to SMSC: 10.149.96.66 at 2775
16-07-2015 05:02:10:ussdgw-1: Bind request: (bindreq: (pdu: 0 9 0 [1]) 900 900 GEN 52 (addrrang: 0 0 2000) ) 
Exception in thread "main" java.lang.NoSuchMethodError: gateway.smpp.PDUEventListener.<init>(Lgateway/smpp/USSDClient;)V
        at gateway.smpp.USSDClient.bind(USSDClient.java:139)
        at gateway.USSDGW.initSmppConnection(USSDGW.java:274)
        at gateway.USSDGW.<init>(USSDGW.java:184)
        at com.vinaphone.app.ttn.USSDDayMore.main(USSDDayMore.java:40)

-bash-3.00$ 

These problems are caused by the concomitant 02 similar class (1 in src, 1 in jar file here is gateway.jar)

查看更多
登录 后发表回答