Elasticsearch java api错误java.lang.NoClassDefFoundError:org / apache / logging / log4j / Logger

时间:2017-05-25 12:41:03

标签: java elasticsearch log4j

我正在使用java elasticsearch 5.4 api。当我运行示例程序时,我得到以下错误

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/logging/log4j/Logger
    at org.elasticsearch.common.logging.Loggers.getLogger(Loggers.java:101)
    at org.elasticsearch.common.xcontent.support.AbstractXContentParser.<init>(AbstractXContentParser.java:57)
    at org.elasticsearch.common.xcontent.json.JsonXContentParser.<init>(JsonXContentParser.java:44)
    at org.elasticsearch.common.xcontent.json.JsonXContent.createParser(JsonXContent.java:103)
    at org.elasticsearch.common.settings.Setting.parseableStringToList(Setting.java:848)
    at org.elasticsearch.common.settings.Setting.lambda$listSetting$27(Setting.java:802)
    at org.elasticsearch.common.settings.Setting.listSetting(Setting.java:807)
    at org.elasticsearch.common.settings.Setting.listSetting(Setting.java:802)
    at org.elasticsearch.common.network.NetworkService.<clinit>(NetworkService.java:50)
    at org.elasticsearch.client.transport.TransportClient.newPluginService(TransportClient.java:98)
    at org.elasticsearch.client.transport.TransportClient.buildTemplate(TransportClient.java:126)
    at org.elasticsearch.client.transport.TransportClient.<init>(TransportClient.java:268)
    at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:125)
    at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:111)
    at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:101)
    at com.ibm.esclient.ESClient.main(ESClient.java:33)
Caused by: java.lang.ClassNotFoundException: org.apache.logging.log4j.Logger
    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)

我的依赖是

<dependencies>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
            <version>5.4.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.8</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.8
            </version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>2.8</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-web</artifactId>
            <version>2.8</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-to-slf4j</artifactId>
            <version>2.8</version>
        </dependency>
        <dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.1.7</version>
</dependency>
    </dependencies>

基本上我试图在这里添加所有可能的log4j记录器,但无济于事。相当令人沮丧,需要帮助!

2 个答案:

答案 0 :(得分:2)

一个小错误导致了这个错误。我指定为测试。我删除它,这开始工作

答案 1 :(得分:0)

以防万一 - 如果正确配置日志记录并且仅在添加ES依赖项后才出现问题,那么仅为ES log4j依赖项添加排除(而不是添加更多记录器)不是更容易的解决方案吗?

在gradle中,它类似于:

compile ('org.elasticsearch.client:elasticsearch-rest-high-level-client:6.2.2'){
    exclude group: 'org.apache.logging.log4j'
}

解决NoClassDefFoundError异常就好了。