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:09

I had a similar problem with my Gradle Project using Intelij. I solved it by deleting the .gradle (see screenshot below) Package and rebuilding the Project. .gradle Package

查看更多
与君花间醉酒
3楼-- · 2018-12-31 02:13

I have just solved this error by restarting my Eclipse and run the applcation. The reason for my case may because I replace my source files without closing my project or Eclipse. Which caused different version of classes I was using.

查看更多
无与为乐者.
4楼-- · 2018-12-31 02:13

I ran into similar issue.

Caused by: java.lang.NoSuchMethodError: com.abc.Employee.getEmpId()I

Finally I identified the root cause was changing the data type of variable.

  1. Employee.java --> Contains the variable (EmpId) whose Data Type has been changed from int to String.
  2. ReportGeneration.java --> Retrieves the value using the getter, getEmpId().

We are supposed to rebundle the jar by including only the modified classes. As there was no change in ReportGeneration.java I was only including the Employee.class in Jar file. I had to include the ReportGeneration.class file in the jar to solve the issue.

查看更多
余欢
5楼-- · 2018-12-31 02:13

The problem in my case was having two versions of the same library in the build path. The older version of the library didn't have the function, and newer one did.

查看更多
孤独总比滥情好
6楼-- · 2018-12-31 02:14

I feel your pain. You can learn programming out of a book, but when it comes to working with Eclipse or Visual Studio, its a ^&^&'n nightmare to do something simple like add a library. Everybody expects you to know how to use it and if you don't they downvote your question. The problem is, if you don't work in an office or know anyone who you can ask these questions, then its almost impossible to figure this stuff out. Anyway...

I was having your problem, and this is how I fixed it. The following steps are a working way to add a library. I had done the first two steps right, but I hadn't done the last one by dragging the ".jar" file direct from the file system into the "lib" folder on my eclipse project. Additionally, I had to remove the previous version of the library from both the build path and the "lib" folder.

If anyone knows of a more proper way to add/update a library, please chime in.

Step 1 - Add .jar to build path

enter image description here

Step 2 - Associate sources and javadocs (optional)

enter image description here

Step 3 - Actually drag .jar file into "lib" folder (not optional)

enter image description here

查看更多
回忆,回不去的记忆
7楼-- · 2018-12-31 02:14

Just adding to existing answers. I was facing this issue with tomcat in eclipse. I had changed one class and did following steps,

  1. Cleaned and built the project in eclpise

  2. mvn clean install

  3. Restarted tomcat

Still I was facing same error. Then I cleaned tomcat, cleaned tomcat working directory and restarted server and my issue is gone. Hope this helps someone

查看更多
登录 后发表回答