本文副标题:解决 NoSuchMethodError 异常

Java代码
  1. java.lang.NoSuchMethodError: org.slf4j.MDC.getCopyOfContextMap()Ljava/util/Map
java.lang.NoSuchMethodError: org.slf4j.MDC.getCopyOfContextMap()Ljava/util/Map

-------------------------------------------------------

 

今天在一台机器上配置完 spring-activemq 后,可以无障碍的运行测试代码。但是,完全相同的代码提交后在另一台机器死活跑不通。主要的错误堆栈信息如下

Java代码
  1. Caused by: java.lang.NoSuchMethodError: org.slf4j.MDC.getCopyOfContextMap()Ljava/util/Map;
  2. at org.apache.activemq.util.MDCHelper.getCopyOfContextMap(MDCHelper.java:30)
  3. at org.apache.activemq.thread.PooledTaskRunner.<init>(PooledTaskRunner.java:42)
Caused by: java.lang.NoSuchMethodError: org.slf4j.MDC.getCopyOfContextMap()Ljava/util/Map;	at org.apache.activemq.util.MDCHelper.getCopyOfContextMap(MDCHelper.java:30)	at org.apache.activemq.thread.PooledTaskRunner.
(PooledTaskRunner.java:42)

 

Java.lang.NoSuchMethodError 的 javadoc 注释如下:

Java代码
  1. /**
  2. * Thrown if an application tries to call a specified method of a
  3. * class (either static or instance), and that class no longer has a
  4. * definition of that method.
  5. * <p>
  6. * Normally, this error is caught by the compiler; this error can
  7. * only occur at run time if the definition of a class has
  8. * incompatibly changed.
  9. *
  10. * @author unascribed
  11. * @version %I%, %G%
  12. * @since JDK1.0
  13. */
/** * 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.  * 

* 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 * @version %I%, %G% * @since JDK1.0 */

 

通过注释( this error can only occur at run time if the definition of a class has incompatibly changed. )可以初步推断是因为版本不兼容导致的。

 

NOTE: 错误信息里方法名后的 Ljava/util/Map 代表的是该方法的返回值类型。

 

再结合所缺失方法(org.slf4j.MDC.getCopyOfContextMap)的 Javadoc:

Java代码
  1. /**
  2. * Return a copy of the current thread's context map, with keys and values of
  3. * type String. Returned value may be null.
  4. *
  5. * @return A copy of the current thread's context map. May be null.
  6. * @since 1.5.1
  7. */
  8. public static Map getCopyOfContextMap() {