0
votes

can somebody help and answer me, why I can not connect and get an error in netbeans. I get the following error message:

Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory at com.datastax.driver.core.Cluster.(Cluster.java:63) at testcassandra.TestCassandra.main(TestCassandra.java:30) Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory 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:423) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:356) ... 2 more

(with Datstax DevCenter I can connect)

Error at cluster = Cluster.builder().addContactPoint("localhost").withPort(9042).build();

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;

public class TestCassandra {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    Cluster cluster;
    Session session;
    // Connect to the cluster and keyspace "demo"
    cluster = Cluster.builder().addContactPoint("localhost").withPort(9042).build();
    session = cluster.connect("demo");
    // Insert one record into the users table
    session.execute("INSERT INTO users (lastname, age, city, email, firstname) VALUES ('Jones', 35, 'Austin', '[email protected]', 'Bob')");
    // Use select to get the user we just entered
    ResultSet results = session.execute("SELECT * FROM users WHERE lastname='Jones'");
    for (Row row : results) {
        System.out.format("%s %d\n", row.getString("firstname"), row.getInt("age"));
    }
    cluster.close();
}

}

1
You seem to be missing the SLF4J library.Kayaman

1 Answers

0
votes

It appears that you may be missing the proper package. You can download the JAR file here That site also has full documentation on how to implement as well.

If you are still having issues after installing verify that you are not using BOTH SLF4J and Log4j 2 as they will cause events to endlessly be routed between them.

Please also make sure you have the correct import statement

Example:

 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;

OR

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;