7

I'm trying to call the Selenium Java libraries from Scala. I'm using Scala IDE (Eclipse), and Scala 2.10.2. What is causing this compiler error?

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)   

Sometimes I fix broken class file errors by including more jars -- jars that javac would not need to see, but apparently scalac does. But is this case I don't know what other jars I can add.

Rob N
  • 15,024
  • 17
  • 92
  • 165
  • Possibly related? https://issues.scala-lang.org/browse/SI-7002 – DaoWen Sep 03 '13 at 19:31
  • I just tried a couple other jar files -- no luck. Seems unlikely they were all compiled with Eclipse. I'll see if I can get the source and manually compile though. – Rob N Sep 03 '13 at 20:08

2 Answers2

4

Found the answer. It's caused by this: https://code.google.com/p/guava-libraries/issues/detail?id=1095. The error disappeared when I added the jsr305 jar.

Rob N
  • 15,024
  • 17
  • 92
  • 165
4

RobN's answer is correct, but I thought I'd write a little bit longer answer with my own experiences. This is related to this question and discussions on Guava issues 776 and 1095 mentioned by RobN.

I had this same problem trying to access

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

Eclipse claims the base64 member does not exist and Gradle build produces the error in the question:

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

The error is caused by optional dependency on some annotations in Guava's pom.xml. As explained in this answer, Java compiler ignores annotations for which corresponding class file is not found, but Scala compiler requires the defitions to compile.

Explicitly adding the dependency that is optional should solve the problem.

In this particular case Guava's pom.xml has following optional dependency and adding the dependency declarations below to your project will solve the problem:

Gradle:

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

Maven:

<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>2.0.2</version>
</dependency>
Community
  • 1
  • 1
Peter Lamberg
  • 8,151
  • 3
  • 55
  • 69
  • I think this is the same as my answer from 5 months ago, though the explanation about Scala is contained in the link. – Rob N Jan 19 '14 at 16:14
  • 2
    @RobN Yes. I did quite a bit of googling to get here. I originally found the post in the link. After knowing the right keywords I searched SO again and found this question. I'm hoping that elaborating a little and crosslinking the questions future explorers will find results faster. I'm not sure if this is a good way to proceed on SO though... – Peter Lamberg Jan 19 '14 at 16:44
  • In my equivalent case the initial error is: `error while loading GoogleService, class file '..../lib/gdata-core-1.0.jar(com/google/gdata/client/GoogleService.class)' is broken`. That [java package](https://code.google.com/p/gdata-java-client/source/checkout) depends on Guava. Somehow I fail to reproduce the solution with sbt using `libraryDependencies += "com.google.code.findbugs" % "jsr305" % "2.0.2"` even though I already had that jar in my classpath (`/lib`) in the first place. Any ideas? – matanster Dec 19 '14 at 21:17