java.lang.NoClassDefFoundError:无法初始化类 com.datastax.driver.core.Cluster

Posted

技术标签:

【中文标题】java.lang.NoClassDefFoundError:无法初始化类 com.datastax.driver.core.Cluster【英文标题】:java.lang.NoClassDefFoundError: Could not initialize class com.datastax.driver.core.Cluster 【发布时间】:2016-08-16 17:39:33 【问题描述】:

我有一个 maven 项目 A

项目 A 的 pom.xml:

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

cassandra-logging 项目的 pom.xml:

<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>

cops-logging 项目的代码如下:

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

【问题讨论】:

您的设置看起来不错。您在 Project A 上的构建成功了吗? 是的。项目 A 能够成功构建 【参考方案1】:

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

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

【讨论】:

【参考方案2】:

将 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>

【讨论】:

【参考方案3】:

我可以通过将 cassandra 依赖版本降级到 &lt;version&gt;2.1.9&lt;/version&gt; 来解决这个问题。不知道这有什么帮助,但我能够继续前进。

【讨论】:

以上是关于java.lang.NoClassDefFoundError:无法初始化类 com.datastax.driver.core.Cluster的主要内容,如果未能解决你的问题,请参考以下文章