NoClassDefFoundError:io / netty / util / Timer

时间:2016-03-08 11:09:48

标签: java cassandra netty

只是想从eclipse尝试Cassandra Java驱动程序并复制“Practical Cassandra”中的示例代码

但面临错误低于eclipse的输出:

Exception in thread "main" java.lang.NoClassDefFoundError: io/netty/util/Timer
    at com.datastax.driver.core.Configuration$Builder.build(Configuration.java:294)
    at com.datastax.driver.core.Cluster$Builder.getConfiguration(Cluster.java:1247)
    at com.datastax.driver.core.Cluster.<init>(Cluster.java:116)
    at com.datastax.driver.core.Cluster.buildFrom(Cluster.java:181)
    at com.datastax.driver.core.Cluster$Builder.build(Cluster.java:1264)
    at SampleApp.connect(SampleApp.java:13)
    at SampleApp.main(SampleApp.java:64)
Caused by: java.lang.ClassNotFoundException: io.netty.util.Timer
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 7 more

这是示例代码:

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

public class SampleApp {
private Cluster cluster;
private Session session;

public void connect(String node) {
    cluster = Cluster.builder().addContactPoint(node).build();
    Metadata metadata = cluster.getMetadata();
    System.out.printf("Cluster: %s\n", metadata.getClusterName());
    for ( Host host : metadata.getAllHosts() ) {
        System.out.printf("Host: %s \n",host.getAddress());
    }
    session = cluster.connect();
}
public void close(){
    cluster.close();
}
public void createSchema(){
    session.execute("CREATE KEYSPACE IF NOT EXISTS portfolio_demo " +
        "WITH REPLICATION 5 { ‘class’: ‘SimpleStrategy’, " +
        "'replication_factor’: 1 };");
    session.execute("CREATE TABLE IF NOT EXISTS portfolio_demo.portfolio (" +
            "portfolio_id UUID, ticker TEXT, " +
            "current_price DECIMAL, current_change DECIMAL, " +
            "current_change_percent FLOAT, " +
            "PRIMARY KEY(portfolio_id, ticker));");
}
public void loadData(){
    session.execute("INSERT INTO portfolio_demo.portfolio " +
            "(portfolio_id, ticker, current_price, " +
            " current_change, current_change_percent) VALUES " +
            "(756716f7-2e54-4715-9f00-91dcbea6cf50, ‘GOOG’, " +
            " 889.07, -4.00, -0.45);");

    session.execute("INSERT INTO portfolio_demo.portfolio " +
            "(portfolio_id, ticker, current_price, " +
            " current_change, current_change_percent) VALUES " +
            "(756716f7-2e54-4715-9f00-91dcbea6cf50, ‘AMZN’, " +
            " 297.92, -0.94, -0.31);");
}
public void printResults(){
    ResultSet results = session.execute("SELECT * FROM " +
            "portfolio_demo.portfolio WHERE portfolio_id 5 " +
            "756716f7-2e54-4715-9f00-91dcbea6cf50;");
    for (Row row : results) {
        System.out.println(String.format("%-7s\t%-7s\t%-7s\t%-7s \n%s",
                "Ticker", "Price", "Change", "PCT",
                "........1........1........1........"));
        System.out.println(String.format("%-7s\t%0.2f\t%0.2f\t%0.2f",
                row.getString("ticker"),
                row.getDecimal("current_price"),
                row.getDecimal("current_change"),
                row.getFloat("current_change_percent") ));
    }
}
public static void main(String[] args) {
    SampleApp client = new SampleApp();
    client.connect("127.0.0.1");
    client.createSchema();
    client.loadData();
    client.printResults();
    client.close();
    }
}

我还添加了几个外部JAR,这些JAR已下载或附带eclipse:

cassandra-driver-core-3.0.0.jar
guava-18.0.jar
netty-3.10.6.Final-20160303.120156-121.jar
org.apache.log4j_1.2.15.v201012070815.jar (from eclipse plugin)
org.slf4j.api_1.7.2.v20121108-1250.jar (from eclipse plugin)
org.slf4j.impl.log4j12_1.7.2.v20131105-2200.jar (from eclipse plugin)

我看到了关于netty错误的相同问题,但仍然无法弄清楚我的代码出了什么问题。

非常感谢。

1 个答案:

答案 0 :(得分:4)

这是错误的Netty版本。驱动程序3.0.0版使用4.0.33。

您可以查看驱动程序的依赖项in the POM(Window->Preferences->General->Workspace)等属性定义为in the parent POM