JUnit为com.google.common.collect.Iterables.tryFind抛出java.lang.NoSuchMethodError

JUnit为com.google.common.collect.Iterables.tryFind抛出java.lang.NoSuchMethodError,java,junit,guava,nosuchmethoderror,Java,Junit,Guava,Nosuchmethoderror,我正在使用Google Guava v13.0,但当我使用包含tryFind的代码运行JUnit测试时,我收到以下消息: java.lang.NoSuchMethodError: com.google.common.collect.Iterables.tryFind(Ljava/lang/Iterable;Lcom/google/common/base/Predicate;)Lcom/google/common/base/Optional; 这似乎只发生在JUnit测试中,因为当生产代码运行时

我正在使用Google Guava v13.0,但当我使用包含tryFind的代码运行JUnit测试时,我收到以下消息:

java.lang.NoSuchMethodError: com.google.common.collect.Iterables.tryFind(Ljava/lang/Iterable;Lcom/google/common/base/Predicate;)Lcom/google/common/base/Optional;
这似乎只发生在JUnit测试中,因为当生产代码运行时没有问题。我正在使用Intellij IDEA v11.1.3,可以导航到guava JAR文件,在com.google.common.collect.Iterable.class中找到tryFind


我见过类似的帖子,但我不确定这与JUnit有什么关系。关于我的问题有什么想法吗?

这种错误通常是由于在类路径上除了尝试使用的较新版本之外,还有一个较旧版本的Guava(甚至是google collections)引起的。运行测试时,尝试检查类路径上的内容。

按照科林的回答,这里有一个很好的方法来检测加载内容的位置:

System.out.println(
    Iterables.class.getProtectionDomain().getCodeSource().getLocation()
);

这将打印出您正在使用的番石榴(或g-c)版本的路径。

您使用maven吗?也许jar不在范围内?这似乎更可能是类路径或构建框架问题,而不是Guava问题。我们不使用Maven。所有库都手动添加到项目中。@atamanroman;顺便说一句,缺少jar永远不会导致
NoSuchMethodError
,因为您第一次得到
NoSuchClassException
@maaartinus true,似乎有不同版本的番石榴included@ColinD回答正确,但这正是帮助我找到它的原因。它位于javaee-reference-6.0-glassfish-3.1.2.jar中。有什么办法吗?@dprice扔掉那个吉安罐。如果你在生产中使用玻璃鱼,它将提供所有这些。对于测试范围,不要包含完整的jee包,只包含您需要的内容。这非常有用。Iterables被埋在MyEclipse插件中!真的很有帮助!我在同时加载google-collections-1.0.jar和guava-20.0.jar时遇到了类似的问题。我不得不删除google-collections-1.0.jar这肯定是问题所在,@Sean下面的代码告诉我问题所在:javaee-reference-6.0-glassfish-3.1.2.jar。你知道如何解决这个问题吗?在我项目的一个依赖项中,我有一个对guava collection版本r03的依赖项。