gpt4 book ai didi

加载正确 JAR 的 java.lang.NoSuchMethodError 异常

转载 作者:行者123 更新时间:2023-11-28 21:56:44 25 4
gpt4 key购买 nike

我正在编写一个 Java 网络应用程序并放入 Tomcat 中运行。

当我尝试使用 JAR 中包含的方法(我确信它包含我需要的方法)时,它显示:

java.lang.NoSuchMethodError: com.couchbase.client.CouchbaseClient.asyncGetBulk(Ljava/util/Collection;)Ljava

但是我尝试通过-verbose:class启动tomcat,我看到如下日志:

[Loaded net.spy.memcached.ops.VBucketAware from file:/.../apache-tomcat-5.5.30/webapps/.../WEB-INF/lib/spymemcached-2.8.2.jar]
[Loaded net.spy.memcached.protocol.binary.SingleKeyOperationImpl from file:/.../apache-tomcat-5.5.30/webapps/.../WEB-INF/lib/spymemcached-2.8.2.jar]
[Loaded net.spy.memcached.protocol.binary.GetOperationImpl from file:/.../apache-tomcat-5.5.30/webapps/.../WEB-INF/lib/spymemcached-2.8.2.jar]
Exception in thread "Thread-2139" java.lang.NoSuchMethodError: com.couchbase.client.CouchbaseClient.asyncGetBulk(Ljava/util/Collection;)Ljava/util/concurrent/Future

虽然spymemcached-2.8.2.jar里面有这个方法,所以我不明白为什么会失败。

这是我调用方法的方式:

Future<Map<String,Object>> f = client.asyncGetBulk(key);

感谢您的帮助。

更新 1:

使用@Narendra Pathai提供的代码,得到如下结果

[
file:/.../WEB-INF/classes/,
file:/.../WEB-INF/lib/activation-1.1.jar,
file:/.../WEB-INF/lib/commons-codec-1.5.jar,
file:/.../WEB-INF/lib/commons-lang-2.2.jar,
file:/.../WEB-INF/lib/commons-logging-1.1.1.jar,
file:/.../WEB-INF/lib/couchbase-client-1.0.3.jar,
file:/.../WEB-INF/lib/jaxb-api-2.1.jar,
file:/.../WEB-INF/lib/jaxb-impl-2.1.9.jar,
file:/.../WEB-INF/lib/jettison-1.1.jar,
file:/.../WEB-INF/lib/netty-3.2.0.Final.jar,
file:/.../WEB-INF/lib/runtime-0.4.1.5.jar,
file:/.../WEB-INF/lib/spymemcached-2.8.2.jar,
file:/.../WEB-INF/lib/stax-api-1.0-2.jar,
file:/.../WEB-INF/lib/xxx_core.jar,
file:/.../WEB-INF/lib/xxx_couchbase.jar
]

它们不包含路径为 com.couchbase.client.CouchbaseClient.asyncGetBulk 的文件除了 couchbase-client-1.0.3.jar。

更新 2:

通过使用代码:

CodeSource codeSource = CouchbaseClient.class.getProtectionDomain().getCodeSource();
if (codeSource != null) {
log.debug(codeSource.getLocation());
}

CodeSource codeSource2 = net.spy.memcached.MemcachedClient.class.getProtectionDomain().getCodeSource();
if (codeSource2 != null) {
log.debug(codeSource2.getLocation());
}

我得到了以下结果:

file:/.../WEB-INF/lib/couchbase-client-1.0.3.jar
file:/.../WEB-INF/lib/spymemcached-2.8.2.jar

看来他们也是对的。

最佳答案

问题出在类路径上

public static void main(String[] args) {
URLClassLoader classLoader = (URLClassLoader)Main.class.getClassLoader();
System.out.println(Arrays.toString(classLoader.getURLs()));
}

在您的 Main 类中使用此方法找出所有 jar 依赖项的解析位置。

查看类加载器是否正在加载旧版本的 jar

关于加载正确 JAR 的 java.lang.NoSuchMethodError 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13642021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com