title: Datastax Java Driver Implementation for Apache Cassandra
body: <p>I am implementing the Datastax Java driver that is described <a href="http://docs.datastax.com/en/developer/java-driver/3.1/" rel="nofollow noreferrer">here</a> in a Maven project in Eclipse.
  I added the three dependencies in pom.xml as described:</p>
  
  <pre><code>&lt;dependency&gt;
    &lt;groupId&gt;com.datastax.cassandra&lt;/groupId&gt;
    &lt;artifactId&gt;cassandra-driver-core&lt;/artifactId&gt;
    &lt;version&gt;3.1.4&lt;/version&gt;
  &lt;/dependency&gt;
  
  &lt;dependency&gt;
    &lt;groupId&gt;com.datastax.cassandra&lt;/groupId&gt;
    &lt;artifactId&gt;cassandra-driver-mapping&lt;/artifactId&gt;
    &lt;version&gt;3.1.4&lt;/version&gt;
  &lt;/dependency&gt;
  
  &lt;dependency&gt;
    &lt;groupId&gt;com.datastax.cassandra&lt;/groupId&gt;
    &lt;artifactId&gt;cassandra-driver-extras&lt;/artifactId&gt;
    &lt;version&gt;3.1.4&lt;/version&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>Then I implemented the class described in <a href="http://docs.datastax.com/en/developer/java-driver/3.1/manual/" rel="nofollow noreferrer">this manual</a>.</p>
  
  <pre><code>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)
          }
  
      }
  
  }
  </code></pre>
  
  <p>And I added the following JAR files to the "Referenced Libraries" directory (Add to Build Path):</p>
  
  <ul>
  <li>cassandra-driver-core-3.1.4.jar</li>
  <li>cassandra-driver-extras-3.1.4.jar</li>
  <li>cassandra-driver-mapping-3.1.4.jar</li>
  </ul>
  
  <p>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.</p>
  
  <pre><code># 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
  </code></pre>
  
  <p><br><br><strong>S O L U T I O N :</strong>
  <br><br>1. Add the following plugin to pom.xml:</p>
  
  <pre><code>    &lt;plugin&gt;
      &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
      &lt;artifactId&gt;maven-shade-plugin&lt;/artifactId&gt;
      &lt;version&gt;2.3&lt;/version&gt;
      &lt;executions&gt;
          &lt;execution&gt;
              &lt;phase&gt;package&lt;/phase&gt;
              &lt;goals&gt;
                  &lt;goal&gt;shade&lt;/goal&gt;
              &lt;/goals&gt;
              &lt;configuration&gt;
                  &lt;transformers&gt;
                      &lt;transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"&gt;
                          &lt;mainClass&gt;Path.to.Main.Class&lt;/mainClass&gt;
                      &lt;/transformer&gt;
                  &lt;/transformers&gt;
              &lt;/configuration&gt;
          &lt;/execution&gt;
      &lt;/executions&gt;
  &lt;/plugin&gt;
  </code></pre>
  
  <p><br>2. Right click on your project -> 'Run As' -> 'Maven Build' -> Type "clean install" -> 'Apply' -> 'Run'.</p>
  
  <p><br>The resulting JAR file will be created under the "target" directory containing all needed dependencies.</p>