Scala错误:类文件损坏,常量池索引错误

yuvru6vn  于 7个月前  发布在  Scala
关注(0)|答案(3)|浏览(129)

我试图从Scala调用Selenium Java库。我使用的是Scala IDE(Eclipse)和Scala 2.10.2。是什么导致了这个编译器错误?

error while loading Function, class file '/Dev/selenium-2.35.0/libs/guava-
14.0.jar(com/google/common/base/Function.class)' is broken 
(class java.lang.RuntimeException/bad constant pool index: 0 at pos: 479)

字符串
有时候我通过包含更多的jar文件来修复损坏的类文件错误--javac不需要看到的jar文件,但显然scalac需要。

qyzbxkaa

qyzbxkaa1#

找到答案了,是这个引起的:https://code.google.com/p/guava-libraries/issues/detail?id=1095。添加jsr305 jar后错误消失了。

polkgigr

polkgigr2#

RobN的回答是正确的,但我想我会写一个更长的答案,结合我自己的经验。这是关于this question和讨论的问题,Guava7761095的RobN提到。
我在试图访问

com.google.common.io.BaseEncoding.base64()

字符串
Eclipse声称base64成员不存在,Gradle构建在问题中产生错误:

[ant:scalac] error: error while loading BaseEncoding, class file 
   '.../guava-16.0.jar(com/google/common/io/BaseEncoding.class)' is broken


该错误是由于Guava的pom.xml中某些注解的optional依赖性引起的。如this answer中所解释的,Java编译器忽略未找到相应类文件的注解,但Scala编译器需要defitions来编译。
显式地添加可选的依赖项应该可以解决这个问题。
在这种特殊情况下,Guava的pom.xml有以下可选依赖项,将下面的依赖项声明添加到您的项目中将解决问题:
Gradle:

compile 'com.google.code.findbugs:jsr305:2.0.2'


梅文:

<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>2.0.2</version>
</dependency>

e5nszbig

e5nszbig3#

在我的例子中,这是Java 8和Java 11之间的区别。该项目被设置为使用Java 8,但scala maven插件使用Java 11。在我的例子中,将JDK_HOME环境变量沿着设置为指向Java 8。

error: scala.reflect.internal.FatalError:
  bad constant pool index: 0 at pos: 7817
     while compiling: <no file>
        during phase: globalPhase=<no phase>, enteringPhase=<some phase>
     library version: version 2.12.17
    compiler version: version 2.12.17
  reconstructed args: -bootclasspath ......jar -d output_path

  last tree to typer: EmptyTree
       tree position: <unknown>
            tree tpe: <notype>
              symbol: null
           call site: <none> in <none>

字符串

相关问题