How do I fix a NoSuchMethodError?

前端 未结 29 2580
孤独总比滥情好
孤独总比滥情好 2020-11-21 05:08

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

相关标签:
29条回答
  • 2020-11-21 05:36

    Most of the times java.lang.NoSuchMethodError is caught be compiler but sometimes it can occur at runtime. If this error occurs at runtime then the only reason could be the change in the class structure that made it incompatible.

    Best Explanation: https://www.journaldev.com/14538/java-lang-nosuchmethoderror

    0 讨论(0)
  • 2020-11-21 05:37

    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.

    0 讨论(0)
  • 2020-11-21 05:39

    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
    0 讨论(0)
  • 2020-11-21 05:39

    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.

    0 讨论(0)
  • 2020-11-21 05:41

    I've had the same problem. This is also caused when there is an ambiguity in classes. My program was trying to invoke a method which was present in two JAR files present in the same location / class path. Delete one JAR file or execute your code such that only one JAR file is used. Check that you are not using same JAR or different versions of the same JAR that contain the same class.

    DISP_E_EXCEPTION [step] [] [Z-JAVA-105 Java exception java.lang.NoSuchMethodError(com.example.yourmethod)]

    0 讨论(0)
  • 2020-11-21 05:42

    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.
    0 讨论(0)
提交回复
热议问题