本文副标题:解决 NoSuchMethodError 异常
- java.lang.NoSuchMethodError: org.slf4j.MDC.getCopyOfContextMap()Ljava/util/Map
java.lang.NoSuchMethodError: org.slf4j.MDC.getCopyOfContextMap()Ljava/util/Map
-------------------------------------------------------
今天在一台机器上配置完 spring-activemq 后,可以无障碍的运行测试代码。但是,完全相同的代码提交后在另一台机器死活跑不通。主要的错误堆栈信息如下
- 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.<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 注释如下:
- /**
- * 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
- * @version %I%, %G%
- * @since JDK1.0
- */
/** * 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:
- /**
- * Return a copy of the current thread's context map, with keys and values of
- * type String. Returned value may be null.
- *
- * @return A copy of the current thread's context map. May be null.
- * @since 1.5.1
- */
- public static Map getCopyOfContextMap() {