编程语言
首页 > 编程语言> > java cassandra connection java.lang.ClassNotFoundException:com.datastax.driver.core.Cluster

java cassandra connection java.lang.ClassNotFoundException:com.datastax.driver.core.Cluster

作者:互联网

我正在尝试使用datastax驱动程序连接到Cassandra.
到目前为止,我刚刚尝试运行教程中提供的SimpleClient应用程序,但我遇到了麻烦.

这是例外

Exception in thread "main" java.lang.NoClassDefFoundError: com/datastax/driver/core/Cluster
    at com.cass.App.connect(App.java:17)
    at com.cass.App.main(App.java:34)
Caused by: java.lang.ClassNotFoundException: com.datastax.driver.core.Cluster
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 2 more

这是我的maven文件

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.cass</groupId>
  <artifactId>Connector</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>Connector</name>
  <url>http://maven.apache.org</url>
  <dependencies>
<dependency>
    <groupId>io.netty</groupId>
    <artifactId>netty-all</artifactId>
    <version>4.0.25.Final</version>
</dependency>
<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>18.0</version>
</dependency>
<dependency>
    <groupId>com.yammer.metrics</groupId>
    <artifactId>metrics-core</artifactId>
    <version>2.2.0</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.7</version>
</dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
<dependency>
  <groupId>com.datastax.cassandra</groupId>
  <artifactId>cassandra-driver-core</artifactId>
  <version>2.0.1</version>
</dependency>

  </dependencies>
<build>
      <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
      </plugins>
    </build>
</project>

这是我的java文件

package com.cass;

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Host;
import com.datastax.driver.core.Metadata;

/**
 * Generate a unique number
 *
 */
public class App 
{

    private Cluster cluster;

public void connect(String node) {
      cluster = Cluster.builder()
            .addContactPoint(node).build();
      Metadata metadata = cluster.getMetadata();
      System.out.printf("Connected to cluster: %s\n", 
            metadata.getClusterName());
      for ( Host host : metadata.getAllHosts() ) {
         System.out.printf("Datatacenter: %s; Host: %s; Rack: %s\n",
               host.getDatacenter(), host.getAddress(), host.getRack());
      }
   }

   public void close() {
      cluster.close();
   }

   public static void main(String[] args) {
      App client = new App();
      client.connect("127.0.0.1");
      client.close();
   }
}

我是Maven和Cassandra的新手.

编辑 – 这是一个愚蠢的错误,不包括cp中依赖的快照
java -cp target / CassandraTest2-1.0-SNAPSHOT-jar-with-dependencies.jar com.Test.App

解决方法:

好的问题是,类路径中不存在所需的jar,或者当您从命令提示符运行时,它无法找到这些jar.

分隔器 ;是为了窗户.在Unix系统上你应该使用:

请按照这两个链接,与答案类似的问题.

java -cp jar1:jar2:jar3:dir1:. MyProgram

Link1link2

标签:java,maven,cassandra,datastax-java-driver
来源: https://codeday.me/bug/20190703/1361813.html