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.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • 1
    Possible duplicate of [Execute jar file with multiple classpath libraries from command prompt](http://stackoverflow.com/questions/13018100/execute-jar-file-with-multiple-classpath-libraries-from-command-prompt) – Ashraful Islam Mar 20 '17 at 17:34
  • @Ashraful It is not. –  Apr 20 '17 at 09:39

1 Answers1

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.

Community
  • 1
  • 1
alien5
  • 76
  • 6
  • Why would we need a plugin to export a JAR that includes dependencies? Isn't that supported by default without any plugin? –  Mar 21 '17 at 16:48
  • 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*. – alien5 Mar 21 '17 at 17:39
  • 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. –  Mar 21 '17 at 18:12
  • You don't have to download them manually, `Maven` does that for you. All the maven plugins can be found in the [Maven Repository](https://mvnrepository.com/) or in [The Central Repository](https://search.maven.org/). You can read more about maven and how maven works in the following [tutorial](https://www.tutorialspoint.com/maven/index.htm) or by checking this [maven in five minutes intro](https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html). – alien5 Mar 21 '17 at 18:44
  • You can also read more about: [maven dependency mechanism](https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html), [maven shade plugin](https://maven.apache.org/plugins/maven-shade-plugin/) and [maven assembly plugin](http://maven.apache.org/plugins/maven-assembly-plugin/). You can find a detailed example with `maven-shade-plugin` [here](https://www.mkyong.com/maven/create-a-fat-jar-file-maven-shade-plugin/) or with `maven-assembly-plugin` [here](https://www.mkyong.com/maven/create-a-fat-jar-file-maven-assembly-plugin/). Hope it helps! – alien5 Mar 21 '17 at 18:54
  • It worked using the maven-shade-plugin, I've updated the question to include the solution. Thank you for your help! –  Mar 24 '17 at 17:03