Search code examples
javadependenciesresolve

Exception when using google guava and google collections at once


My java code is dependent on 2 libraries A and B

A has a dependency on GoogleCollections B has a dependency GoogleGuava r10;

Now when i build  my code everything works fine.But when i run i get following exception

java.lang.NoSuchMethodError: com.google.common.collect.ImmutableList.copyOf([Ljava/lang/Object;)Lcom/google/common/collect/ImmutableList;
    at com.abc.Pqr$Builder.withXYZ(ExponentialBackoffRetryPolicy.java:329)

How can i solve this problem?


Solution

  • Google Collections has been deprecated and moved into Guava. Exclude it from A. With Maven, you do that with the exclusions tag below the dependency tag in your project's POM:

        <dependency>
            <groupId>org.project</groupId>
            <artifactId>library-a</artifactId>
            <version>[version]</version>
            <exclusions>
                <exclusion>
                    <!-- whatever the correct values are -->
                    <artifactId>google-collections</artifactId>
                    <groupId>google-collections</groupId>
                </exclusion>
            </exclusions>
        </dependency>