java.lang.NoClassDefFoundError: com/google/common/

2020-02-01 07:42发布

问题:

I'm seeing the following error :

java.lang.NoClassDefFoundError: com/google/common/util/concurrent/FutureFallback

when the cluster.connect() is called :

            String hosts = CassandraClientUtil.getHost();
            String localDC = CassandraClientUtil.getLocalDC();
            Cluster cluster = null;
            if (StringUtils.isNotEmpty(localDC))
            {
                cluster = Cluster.builder().addContactPoints(hosts.split(","))
                        .withCredentials(CassandraCopsComponentLogger.USER_NAME, CassandraCopsComponentLogger.AUTH_CODE)
                        .withQueryOptions(new QueryOptions().setConsistencyLevel(ConsistencyLevel.LOCAL_ONE))
                        .withLoadBalancingPolicy(new TokenAwarePolicy(DCAwareRoundRobinPolicy.builder().withLocalDc(localDC).build())).build();
            }
            else
            {
                cluster = Cluster.builder().addContactPoints(hosts.split(","))
                        .withCredentials(CassandraCopsComponentLogger.USER_NAME, CassandraCopsComponentLogger.AUTH_CODE)
                        .withQueryOptions(new QueryOptions().setConsistencyLevel(ConsistencyLevel.LOCAL_ONE)).build();
            }

            Session session = cluster.connect();
            CassandraCopsComponentLogger.mappingManager = new MappingManager(session);

The pom.xml has the following dependencies :

<dependencies>
        <dependency>
                <groupId>com.google.guava</groupId>
                <artifactId>guava</artifactId>
                <version>16.0.1</version>
        </dependency>
        <dependency>
            <groupId>com.datastax.cassandra</groupId>
            <artifactId>cassandra-driver-core</artifactId>
            <version>2.1.9</version>
        </dependency>
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty</artifactId>
            <version>3.9.0.Final</version>
        </dependency>
        <dependency>
            <groupId>com.codahale.metrics</groupId>
            <artifactId>metrics-core</artifactId>
            <version>3.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.5</version>
        </dependency>   
        <dependency>
            <groupId>com.datastax.cassandra</groupId>
            <artifactId>cassandra-driver-mapping</artifactId>
            <version>2.1.9</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.3.1</version>
        </dependency>
  </dependencies>

I saw a post on stackoverflow here where they recommended to upgrade the guava version to 16.0.1 but that did not help me solve my problem. Some directions from here will be really helpful as I'm new to cassandra. To add more background this thing works as a standalone project, when I include this project as a maven dependency to some other project it raises this runtime error.

回答1:

com.google.common.util.concurrent.FutureFallback is deprecated in Guava 19.0 and removed since Guava 20.0.

Use Guava 19.0 and do not use Guava 20.0 or greater, until you upgrade the Cassandra driver.



回答2:

I updated the Cassandra driver version to latest available and it should fix the issue.

<dependency>
   <groupId>com.datastax.cassandra</groupId>
   <artifactId>cassandra-driver-core</artifactId>
   <version>3.5.0</version>
</dependency>


回答3:

Don't add external guava version . whatever datastax-cassandra-core using only you can put that version . otherwise don't need of that .



回答4:

If anyone like me didn't know, that there is a new version (4.x) out there with a new and different group id, take a look at the quickstart. This new version still uses Guava however it's shaded.

The driver now requires Java 8. It does not depend on Guava anymore (we still use it internally but it's shaded).

More information can be found in the upgrade guide.