代码时出现IncompatibleClassChangeErro

代码时出现IncompatibleClassChangeErro

本文介绍了使用sbt测试番石榴代码时出现IncompatibleClassChangeError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用maven和sbt的Java 1.6 + Scala 2.9.2项目.当我添加了番石榴缓存类时,项目开始在测试中抛出此异常:

I have a Java 1.6 + Scala 2.9.2 project using maven and sbt. The project started throwing this exception in the tests when I added the guava Cache classes:

java.lang.IncompatibleClassChangeError:
class com.google.common.cache.CacheBuilder$3 has interface
com.google.common.base.Ticker as super class

执行sbt clean compile效果很好,但是sbt clean test抛出此异常.

Doing sbt clean compile works perfectly but sbt clean test throws this exception.

番石榴的版本是13.0

The version of guava is 13.0

引发异常的模块具有:

import com.google.common.cache.{CacheBuilder, Cache}
...
val cache: Cache[String, Integer] = CacheBuilder.newBuilder()
     .maximumSize(5000).build()

我的sbt依赖项是:

libraryDependencies ++= Seq(
    "org.scala-tools.testing" %% "scalacheck" % "1.9" % "test" changing(),
    "org.specs2" %% "specs2" % "1.9" % "test"
)

与Maven的依赖关系是:

And the maven dependencies are:

<dependencies>
    <dependency>
      <groupId>org.apache.thrift</groupId>
      <artifactId>libthrift</artifactId>
      <version>0.8.0</version>
    </dependency>
    <dependency>
      <groupId>com.google.guava</groupId>
      <artifactId>guava</artifactId>
      <version>13.0</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.6.6</version>
    </dependency>
    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.0.0</version>
      <type>jar</type>
    </dependency>
    <dependency>
      <groupId>com.googlecode.json-simple</groupId>
      <artifactId>json-simple</artifactId>
      <version>1.1</version>
    </dependency>
    <dependency>
      <groupId>org.apache.zookeeper</groupId>
      <artifactId>zookeeper</artifactId>
      <version>3.3.1</version>
    </dependency>
    <dependency>
      <groupId>org.scalatest</groupId>
      <artifactId>scalatest_2.9.1</artifactId>
      <version>1.8</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.8</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-all</artifactId>
      <version>1.9.0</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.hamcrest</groupId>
      <artifactId>hamcrest-all</artifactId>
      <version>1.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.novocode</groupId>
      <artifactId>junit-interface</artifactId>
      <version>0.8</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

推荐答案

代码从Guava 13之前的Interface更改为Abstract class.

Ticker was changed from Interface to abstract class before Guava 13.

这是在测试中发生但未编译的事实,这是一个很好的指标.

The fact that this happens in test but not compile is a good indicator.

我认为Guava的早期版本最终会出现在您的测试类路径中,可能是传递依赖.

I think an earlier version of Guava is ending up on your test classpath, probably as a transitive dependency.

您可以尝试使用 sbt-inspectr 探索测试类路径并查找更多信息,或者您可以尝试排除,例如:

You could try using sbt-inspectr to explore the test classpath and find out more, or you could try excluding, something like:

"org.scala-tools.testing" %% "scalacheck" % "1.9" % "test" changing(), excludeAll(
    ExclusionRule(organization = "com.google.guava")
)

这篇关于使用sbt测试番石榴代码时出现IncompatibleClassChangeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 13:41