Hot questions for Handling Error in Guava

Top 10 Java Open Source / Guava / Handling Error

IllegalAccessError to guava's StopWatch from org.apache.hadoop.mapreduce.lib.input.FileInputFormat.listStatus

Question: I'm trying to run small spark application and am getting the following exception:

Exception in thread "main" java.lang.IllegalAccessError: tried to access method com.google.common.base.Stopwatch.<init>()V from class org.apache.hadoop.mapreduce.lib.input.FileInputFormat
    at org.apache.hadoop.mapreduce.lib.input.FileInputFormat.listStatus(FileInputFormat.java:262)
    at org.apache.hadoop.mapreduce.lib.input.CombineFileInputFormat.getSplits(CombineFileInputFormat.java:217)
    at org.apache.spark.rdd.NewHadoopRDD.getPartitions(NewHadoopRDD.scala:95)
    at org.apache.spark.rdd.RDD$$anonfun$partitions$2.apply(RDD.scala:219)
    at org.apache.spark.rdd.RDD$$anonfun$partitions$2.apply(RDD.scala:217)
    at scala.Option.getOrElse(Option.scala:120)

the relevant gradle dependencies section:

compile('org.apache.spark:spark-core_2.10:1.3.1')
compile('org.apache.hadoop:hadoop-mapreduce-client-core:2.6.2') {force = true}
compile('org.apache.hadoop:hadoop-mapreduce-client-app:2.6.2') {force = true}
compile('org.apache.hadoop:hadoop-mapreduce-client-shuffle:2.6.2') {force = true}
compile('com.google.guava:guava:19.0') { force = true }

Answer: version 2.6.2 of hadoop:hadoop-mapreduce-client-core can't be used together with guava's new versions (I tried 17.0 - 19.0) since guava's StopWatch constructor can't be accessed (causing above IllegalAccessError)

using hadoop-mapreduce-client-core's latest version - 2.7.2 (in which they don't use guava's StopWatch in the above method, rather they use org.apache.hadoop.util.StopWatch) solved the problem, with two additional dependencies that were required:

compile('org.apache.hadoop:hadoop-mapreduce-client-core:2.7.2') {force = true}

compile('org.apache.hadoop:hadoop-common:2.7.2') {force = true} // required for org.apache.hadoop.util.StopWatch  

compile('commons-io:commons-io:2.4') {force = true} // required for org.apache.commons.io.Charsets that is used internally

Note: there are two org.apache.commons.io packages: commons-io:commons-io (ours here), and org.apache.commons:commons-io (old one, 2007). make sure to include the correct one.


NoSuchMethodError: com.google.common.util.concurrent.MoreExecutors.directExecutor conflits on Elastic Search jar

Question: While creating Elasticsearch Client, I'm getting the exception "java.lang.NoSuchMethodError: com.google.common.util.concurrent.MoreExecutors.directExecutor()Ljava/util/concurrent/Executor;" After some lookup, seams like the Guava-18 is being overwrite by an older version at runtime, and Guava-18 only works during compile task.

My Maven configuration is the follow:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.0</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.4.1</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

How can i force the Guava-18 version at execution time?

Answer: You should try to find where the "old" version of guava is taken from and to exclude it once for all.

Find the dependency:

mvn dependency:tree | grep guava

Exclude it:

<dependency>
  <groupId>org.whatever</groupId>
  <artifactId>the_lib_that_includes_guava</artifactId>
  <version>0.97</version>
  <exclusions>
    <exclusion>
      <artifactId>com.google</artifactId>
      <groupId>guava</groupId>
    </exclusion>
  </exclusions>
</dependency>

Maven "could not parse error message" (Java 7 + Maven 2)

Question: I have a maven-based GWT project that includes Guava. I am running into trouble with Maven trying (and failing) to compile the sources that it finds in guava-gwt*.jar:

could not parse error message:   symbol:   static setCountImpl
  location: class
/home/mark/.m2/repository/com/google/guava/guava-gwt/11.0.1/guava-gwt-11.0.1.jar(com/google/common/collect/AbstractMultiset.java):100: error: cannot find symbol
    return setCountImpl(this, element, count);

I can't figure out why Maven thinks it needs to compile the sources in guava-gwt. Here's what my project looks like:

├── pom.xml
└── src
    ├── main
       └── java
    └── test
        └── java
            └── SomeTestFile.java

SomeTestFile.java

public class SomeTestFile {
    @Test
    public void testMethod() {
        Multimap<Integer, String> someMap = ArrayListMultimap.create();
        someMap.put(5, "five");
        System.out.println(someMap);
    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>guava-problem</groupId>
    <artifactId>guava-problem</artifactId>
    <version>1.0</version>

    <dependencies>

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>11.0.1</version>
        </dependency>

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava-gwt</artifactId>
            <version>11.0.1</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.2</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

I have already tried the following:

1. Removing the guava dependency (leaving only guava-gwt)

2. Scoping guava-gwt to provided

I'm not sure what else to try. guava-gwt includes sources because GWT will compile it into equivalent Javascript. But I don't want Maven to try to compile these sources.

Answer: The exact pom file along with the test class above compiles fine on my Windows box with maven 3.0.4.

The problem could be with the maven version that you are using. Or there could be other maven goals in the actual pom, which may be causing an issue.


Guava: java.lang.NoClassDefFoundError - com.google.common.collect.HashBiMap

Question: I currently face the problem of java.lang.NoClassDefFoundError: com.google.common.collect.HashBiMap when using guava libraries

I already add guava-12.0.jar into my project as a reference library but I still get the error. May you give some advice on what the problem would be? Thankyou for your help

public class MainActivity extends Activity{

     private BiMap<String,String>  bidiMap; 

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);     
          setContentView(R.layout.bible_help_cal);

        bidiMap = HashBiMap.create();           
        bidiMap.put("a","100");
        bidiMap.put("b","200");
    }
}

Error Message I get

05-29 18:35:19.737: E/AndroidRuntime(376): FATAL EXCEPTION: main
05-29 18:35:19.737: E/AndroidRuntime(376): java.lang.NoClassDefFoundError: com.google.common.collect.HashBiMap
05-29 18:35:19.737: E/AndroidRuntime(376):  at my.project.MainActivity.onCreate(MainActivity.java:18)
05-29 18:35:19.737: E/AndroidRuntime(376):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
...

Answer: This error means that the class was available at compile time, but cannot be found during run time. It most commonly happens when your compile time classpath is different from your runtime classpath.

It is very likely that your runtime classpath doesn't contain the guava jar. To verify this, try printing your classpath in your code.

on the command line, you can use: java -cp "path/to/guava.jar" MyMainClass

or alternatively, set the CLASSPATH environment variable to include the jar.


How to remedy error caused by guava: Program type already present: com.google.common.util.concurrent.internal.InternalFutures

Question: An AAR library already uses com.google.guava.

If an app includes the following in its build.gradle:

api 'com.google.guava:guava:27.0-android'

Building the app generates the following error:

Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'.
> com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives: ...
  Learn how to resolve the issue at https://developer.android.com/studio/build/dependencies#duplicate_classes.
  Program type already present: com.google.common.util.concurrent.internal.InternalFutures

If I do not include "api 'com.google.guava:guava:27.0-android'", the app can be built, but it has runtime error of java.lang.NoClassDefFoundError when it reaches the point of using the Guava method: Iterables.find

Answer: Since Guava 27.0, ListenableFuture is located in separate artifact, see the announcement. You can try two things (one at a time):

  1. Exclude "listenablefuture" module (group "com.google.guava") and build your project again.
  2. I don't know the AAR specifics, but it could be that 27.0-android doesn't work with AAR, so you should try 27.0.1-android or higher instead.
implementation 'com.google.guava:guava:27.0.1-android'