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

pJAEY picture pJAEY · Jan 12, 2015 · Viewed 9k times · Source

I am trying to connect to Cassandra with datastax driver. So far I have just tried to run the SimpleClient app available in tutorials but I am having troubles.

Here is the Exception

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

Here is my maven file

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

Here is my java file

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();
   }
}

I am new to both Maven and Cassandra.

Edit - it was a stupid mistake of not including snapshot with dependencies in the cp java -cp target/CassandraTest2-1.0-SNAPSHOT-jar-with-dependencies.jar com.Test.App

Answer

Ankur Singhal picture Ankur Singhal · Jan 12, 2015

Ok the issue is that the required jar is not present in classpath or when you are running from command prompt, it is not able to locate those jars.

Separator ; is for windows. On Unix systems you should use :

please follow these two links, similar issues with answers.

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

Link1 and link2