4

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

3
  • how are you running it.. Commented Jan 12, 2015 at 4:12
  • java -cp target/Connector-1.0-SNAPSHOT.jar com.cass.App Commented Jan 12, 2015 at 4:21
  • did you solve it ? I had a similar problem. Could you update the question in case you solved? thanks Commented May 29, 2021 at 15:01

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

5 Comments

Wouldn't maven handle the dependencies?
@pJAEY Maven will download the jars and dependent jars, but you need to referenced it in your classpath.
I'd recommend looking into the maven assembly plugin and jar-with-dependencies to build a jar that includes all dependencies. Another option is to use maven itself to run your main class using the exec:java goal to run your main class.
Didn't change anything. I fixed it by trying java -cp target/CassandraTest2-1.0-SNAPSHOT-jar-with-dependencies.jar com.Test.App
Good catch on the classpaths @ankur-singhal. thanks for the help :)
0

I had the same issue. Only difference was datastax version was 2.1.1. Later i realized this version has an issue. My project started working after changing the version from 2.1.1 to 2.1.9 (In fact I tested your version too. it was working. Other that this version, it worked on all others. Strang!)

And for the maven. Yes, it does handle the dependency. Normally, if you put the dependency in pom file and run the class it handles. It did for me too.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.