1

The error is as the following:

ERROR 1912 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler dispatch failed; nested exception is java.lang.BootstrapMethodError: java.lang.NoClassDefFoundError: com/datastax/oss/protocol/internal/SegmentCodec] with root cause

java.lang.ClassNotFoundException: com.datastax.oss.protocol.internal.SegmentCodec
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382) ~[na:1.8.0_251]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:418) ~[na:1.8.0_251]
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355) ~[na:1.8.0_251]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:351) ~[na:1.8.0_251]
    at com.datastax.oss.driver.internal.core.context.DefaultDriverContext.<init>(DefaultDriverContext.java:170) ~[java-driver-core-shaded-4.13.0.jar:na]
    at com.datastax.oss.driver.api.core.session.SessionBuilder.buildContext(SessionBuilder.java:968) ~[java-driver-core-shaded-4.13.0.jar:na]

And code is as:

public List<String> getCassandraTables(String cassandraHost,String cassandraUsername,String cassandraPassword,String datacenter,String cassandraKeyspace){
      try{
            
        InetSocketAddress address = new InetSocketAddress(cassandraHost,9042);

        DriverConfigLoader loader = DriverConfigLoader.programmaticBuilder()
        .withString(DefaultDriverOption.AUTH_PROVIDER_USER_NAME,cassandraUsername)
        .withString(DefaultDriverOption.AUTH_PROVIDER_PASSWORD,cassandraPassword).build();

        CqlSession builder = CqlSession.builder()
        .addContactPoint(address)
        .withConfigLoader(loader)
        .withKeyspace(cassandraKeyspace)
        .withLocalDatacenter(datacenter)
        .build();

        Metadata metadata = builder.getMetadata();
        KeyspaceMetadata keyspaceMetadata = metadata.getKeyspace(cassandraKeyspace)
        .orElseThrow(() -> new RuntimeException("Keyspace not found: " + cassandraKeyspace));
        List<String> tableNames = keyspaceMetadata.getTables().values().stream()
        .map(table->table.getName().toString()).collect(Collectors.toList());

        return tableNames;
        
        } catch(Exception e) {
            System.out.println("Failed to get tables from the Cassandra cluster: "+e.getMessage());
        return null;
     }
}
Christian Baumann
  • 3,188
  • 3
  • 20
  • 37
Balaji4you
  • 21
  • 1
  • Can you show your pom.xml so we can see dependencies? – Paul Apr 28 '23 at 19:24
  • Was this line run? I don't see it in your output: `System.out.println("Failed to get tables from the Cassandra cluster: "+e.getMessage());` – Aaron Apr 28 '23 at 19:59
  • Also, possible dupe: https://stackoverflow.com/questions/64327809/cassandra-springboot-java-lang-classnotfoundexception-com-datastax-oss-proto – Aaron Apr 28 '23 at 20:02

1 Answers1

0

It is not clear from your post:

  • what you've configured as the cassandraHost, and
  • what the full error message + full stack trace is.

However if this is the same problem you've raised in Unable to connect to cluster from Spring Boot app, getting ConnectionInitException: "Could not reach any contact point", then I believe the issue is that you have specified the wrong host as localhost instead of the IP address that Cassandra is using to listen for client connections. You should use the same address as you've configured for rpc_address in cassandra.yaml. Cheers!

Erick Ramirez
  • 13,964
  • 1
  • 18
  • 23