user5487236
user5487236

Reputation:

Datastax Java Driver Implementation for Apache Cassandra

I am implementing the Datastax Java driver that is described here in a Maven project in Eclipse. I added the three dependencies in pom.xml as described:

<dependency>
  <groupId>com.datastax.cassandra</groupId>
  <artifactId>cassandra-driver-core</artifactId>
  <version>3.1.4</version>
</dependency>

<dependency>
  <groupId>com.datastax.cassandra</groupId>
  <artifactId>cassandra-driver-mapping</artifactId>
  <version>3.1.4</version>
</dependency>

<dependency>
  <groupId>com.datastax.cassandra</groupId>
  <artifactId>cassandra-driver-extras</artifactId>
  <version>3.1.4</version>
</dependency>

Then I implemented the class described in this manual.

package CasConnector.CasConnector;
import com.datastax.driver.core.*;

public class CasConnector {

    public static void main(String[] args) {

        Cluster cluster = null;
        try {
            cluster = Cluster.builder()                                                    // (1)
                    .addContactPoint("127.0.0.1")
                    .build();
            Session session = cluster.connect();                                           // (2)

            ResultSet rs = session.execute("select release_version from system.local");    // (3)
            Row row = rs.one();
            System.out.println(row.getString("release_version"));                          // (4)
        } finally {
            if (cluster != null) cluster.close();                                          // (5)
        }

    }

}

And I added the following JAR files to the "Referenced Libraries" directory (Add to Build Path):

Then I exported the project as JAR file to execute on Linux, but I got the error below. Seems the Cluster class cannot be loaded, but I have added the cassandra-driver-core-3.1.4 dependency in the Maven project which includes it. Please suggest what could be wrong or missing in my config.

# java -jar test.jar
Exception in thread "main" java.lang.NoClassDefFoundError: com/datastax/driver/core/Cluster
        at CasConnector.CasConnector.CasConnector.main(CasConnector.java:14)
Caused by: java.lang.ClassNotFoundException: com.datastax.driver.core.Cluster
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 1 more



S O L U T I O N :

1. Add the following plugin to pom.xml:

    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.3</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>Path.to.Main.Class</mainClass>
                    </transformer>
                </transformers>
            </configuration>
        </execution>
    </executions>
</plugin>


2. Right click on your project -> 'Run As' -> 'Maven Build' -> Type "clean install" -> 'Apply' -> 'Run'.


The resulting JAR file will be created under the "target" directory containing all needed dependencies.

Upvotes: 2

Views: 2352

Answers (1)

alien5
alien5

Reputation: 76

I think that you are missing some 3rd party dependencies from your test.jar. If you are using Maven to build your project, try to use a maven plugin to create the JAR instead of using the Eclipse export project as jar feature. To create an executable JAR with dependencies using Maven you can use the maven-shade-plugin or maven-assembly-plugin maven plugins.

Upvotes: 0

Related Questions