2

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

  • cassandra-driver-core-3.1.4.jar
  • cassandra-driver-extras-3.1.4.jar
  • cassandra-driver-mapping-3.1.4.jar

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.

2

1 Answer 1

0

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.

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

6 Comments

Why would we need a plugin to export a JAR that includes dependencies? Isn't that supported by default without any plugin?
If you are asking that exporting a JAR which includes its dependencies is supported by default in Eclipse, then the answer is no. Eclipse will only package your project with along with the jars that you provided. I understood from your post that even though you have a maven project, you manually added the JAR files to the "Referenced Libraries" directory (Add to Build Path) but this doesn't add also the transitive dependencies that those jars need. Using Maven to build and package your project you're leveraging the Maven Dependency Resolution Mechanism.
Thank you for the clarification. The link you referenced isn't so clear for me. Can you tell me where can I find the plugin software (download) and how to set it up. A detailed description would be great.
You don't have to download them manually, Maven does that for you. All the maven plugins can be found in the Maven Repository or in The Central Repository. You can read more about maven and how maven works in the following tutorial or by checking this maven in five minutes intro.
You can also read more about: maven dependency mechanism, maven shade plugin and maven assembly plugin. You can find a detailed example with maven-shade-plugin here or with maven-assembly-plugin here. Hope it helps!
|

Your Answer

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