尝试使用Elasticsearch Transport Client时出错:未加载到类路径的依赖项

时间:2016-11-02 04:34:54

标签: java elasticsearch

我使用以下代码在Elasticsearch上创建客户端:

Settings settings = Settings.builder()
    .put("cluster.name", "myClusterName")
    .build();
TransportClient client = new PreBuiltTransportClient(settings)
    .addTransportAddress(newInetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));

我收到了这些错误:

Exception in thread "main" java.lang.NoClassDefFoundError: org/elasticsearch/plugins/ActionPlugin
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at org.elasticsearch.transport.client.PreBuiltTransportClient.<clinit>(PreBuiltTransportClient.java:54)
    at exadatum.com.exadatum.elastic.testSearch.main(testSearch.java:30)
Caused by: java.lang.ClassNotFoundException: org.elasticsearch.plugins.ActionPlugin
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 14 more

1 个答案:

答案 0 :(得分:9)

更新:现在记录在

确保已将客户端依赖项添加到类路径中:

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>transport</artifactId>
    <version>5.0.0-rc1</version>
</dependency>

此依赖项可能无法从子依赖项传递加载。

您可能还会发现主要的Elasticsearch和Apache Logging工件也需要在类路径上:

<dependency>
    <groupId>org.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
    <version>5.0.0-rc1</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.7</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.7</version>
</dependency>