1、 錯誤內容:java.lang.NoSuchMethodError: com.Boot: method <init>()V not found
此提示內容指,com.Boot沒有參數為空的構造函數。提示中指明了異常所在的類和對應的函數。
2、 java文檔: 說的很清楚了,是類的變化不兼容導致的異常。通俗的說法是,調用方使用的類定義和加載的類定義不一樣,加載的類實際上沒有將要調用的方法。
/** * Thrown if an application tries to call a specified method of a * class (either static or instance), and that class no longer has a * definition of that method. * <p> * Normally, this error is caught by the compiler; this error can * only occur at run time if the definition of a class has * incompatibly changed. * * @author unascribed * @since JDK1.0 */ public class NoSuchMethodError extends IncompatibleClassChangeError {
3、 如何制造一個NoSuchMethodError:
這里提供兩個java代碼。放在com目錄下。依次執行以下命令:
javac com/Boot.java javac com/Cloud.java vim com/Boot.java 把Boot的無參構造函數注釋掉,把有參構造函數的注釋打開 javac com/Boot.java 重新編譯Boot的類文件,最終結果沒有無參構造函數 java com/Cloud 此處會出現NoSuchMethodError異常
方法提供方 package com; public class Boot{ private String name; // public Boot (String name){ // this.name = name; // } 注意這里的兩個構造函數。 public Boot (){ } public void run(){ System.out.println(name); } } ====================== 調用方 package com; import com.Boot; public class Cloud{ public Cloud (){ } public static void main(String[] args){ new Boot().run(); } }
4、原因說明
實際使用的類文件不配套導致的異常。即通常所說的jar包版本不一致導致此問題。
5、實際舉例
Spring Boot 2.x 使用 feign 出現此錯誤。Spring Cloud 1.1調用的構造函數是 new SpringApplicationBuilder(new Object[0]) ,但Spring Boot 2.1 提供的構造函數只有一個
public SpringApplicationBuilder(Class... sources) {
this.application = this.createSpringApplication(sources);
}
java.lang.NoSuchMethodError: org.springframework.boot.builder.SpringApplicationBuilder.<init>([Ljava/lang/Object;)V at org.springframework.cloud.bootstrap.BootstrapApplicationListener.bootstrapServiceContext(BootstrapApplicationListener.java:120) at org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:84) at org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:62) at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:127) at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:76) at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:53) at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:342) at org.springframework.boot.SpringApplication.run(SpringApplication.java:305) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1204) at com.Application.main(Application.java:22)
`
6、解決方式:換依賴jar的版本