java.lang.NoClassDefFoundError: Could not initialize class com.datastax.driver.core.Cluster

java.lang.NoClassDefFoundError: Could not initialize class com.datastax.driver.core.Cluster

我有一个maven项目A

pom.xml 项目 A:

<dependencies>
       <dependency> 
            <groupId>com.app.cops</groupId>
            <artifactId>cassandra-logging</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
</dependencies>

pom.xml 项目 cassandra-logging:

<dependencies>
        <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-core</artifactId>
            <version>3.0.0</version>
        </dependency>   
        <dependency>
            <groupId>com.datastax.cassandra</groupId>
            <artifactId>cassandra-driver-mapping</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>19.0</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.3.1</version>
        </dependency>
  </dependencies>

警察记录项目具有以下代码:

CassandraCopsComponentLogger.instance = new CassandraCopsComponentLogger();

                String hosts = CassandraClientUtil.getHost();
                String localDC = CassandraClientUtil.getLocalDC();
                Cluster cluster;
                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);

我不断收到以下行的异常:

cluster = Cluster.builder().addContactPoints(hosts.split(","))

我在 cassandra-logging 项目中进行了单元测试,它可以很好地处理这段代码。但是当我从项目 A 调用相同的代码时,我得到

java.lang.NoClassDefFoundError: Could not initialize class com.datastax.driver.core.Cluster

我能够通过将 cassandra 依赖版本降级到 <version>2.1.9</version> 来解决这个问题。不确定这有何帮助,但我能够继续前进。

将 cassandra-driver-core 和 cassandra-driver-mapping 的版本更新到 3.3.0 - 这对我有用:

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

我运行遇到了类似的问题。我得到的错误是:

Factory method 'sqlSession' threw exception; 
nested exception is java.lang.NoClassDefFoundError: com/datastax/oss/protocol/internal/SegmentCodec

相关 pom.xml 细分:

<properties>
..
<oss-java-driver.version>4.9.0</oss-java-driver.version>
..
</properties>
..
<dependency>
    <groupId>com.datastax.oss</groupId>
    <artifactId>java-driver-core</artifactId>
    <version>${oss-java-driver.version}</version>
</dependency>
<dependency>
    <groupId>com.datastax.oss</groupId>
    <artifactId>java-driver-query-builder</artifactId>
    <version>${oss-java-driver.version}</version>
</dependency>

对我有用的解决方案:我必须明确声明对 native-protocol 的依赖才能覆盖 native-protocol 的版本。

<properties>
..
<cassandra-native-protocol.version>1.4.11</cassandra-native-protocol.version>
..
</properties>
..
<dependency>
    <groupId>com.datastax.oss</groupId>
    <artifactId>native-protocol</artifactId>
    <version>${cassandra-native-protocol.version}</version>
</dependency>

参考:https://community.datastax.com/answers/8185/view.html