Java cassandra connection java.lang.ClassNotFoundException: com.datastax.driver.core.Cluster
I am trying to connect to Cassandra with datastax driver. Until now, I was just trying to start the SimpleClient application available in the tutorials, but I'm having problems.
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 silly mistake not including the dependency snapshot in cp
java -cp target/CassandraTest2-1.0-SNAPSHOT-jar-with-dependencies.jar com.Test.App
source to share
Ok, the problem is that the required jar is missing in the classpath or when you run from the command line it cannot find those jars.
The separator is ;
designed for windows
. On Unix systems
you must use:
please follow these two links, similar questions and answers.
java -cp jar1:jar2:jar3:dir1:. MyProgram
source to share
I had the same problem. The only difference was datastax version - 2.1.1. Later I realized that this version has a problem. My project started working after changing version from 2.1.1 to 2.1.9 (actually I tested your version too, it worked. Other, this version worked on everyone else. Strang!)
And for maven. Yes, he copes with addiction. Usually if you put the dependency in the pom file and run the class that it handles. It was for me too.
source to share