简体   繁体   中英

How to resolve java.lang.NoClassDefFoundError: org/apache/logging/log4j/Logger while the class is actually accessible

I'm trying to setup a project using Maven wherein two incompatible projects are used (Elasticsearch and Neo4j). These two projects both make use of Lucene but different and incompatible versions. As it turned out this is impossible! Fortunately, I need Elasticsearch at runtime and Neo4j at test time. So theoretically, I should be fine. But I'm not!

As for the shared Lucence package, Neo4j makes use of an older version and because of that I explicitly named the older versions in <dependencyManager> with <scope>test</scope> . Lots of runtime errors have been resolved this way. But now, I'm facing a NoClassDefFoundError exception running tests, within Elasticsearch 's file saying:

java.lang.NoClassDefFoundError: org/apache/logging/log4j/Logger
    at org.elasticsearch.common.logging.Loggers.getLogger(Loggers.java:101)
    at org.elasticsearch.search.suggest.completion2x.Completion090PostingsFormat.<clinit>(Completion090PostingsFormat.java:78)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at java.lang.Class.newInstance(Class.java:442)
    at org.apache.lucene.util.NamedSPILoader.reload(NamedSPILoader.java:72)
    at org.apache.lucene.util.NamedSPILoader.<init>(NamedSPILoader.java:51)
    at org.apache.lucene.util.NamedSPILoader.<init>(NamedSPILoader.java:38)
    at org.apache.lucene.codecs.PostingsFormat$Holder.<clinit>(PostingsFormat.java:49)
    at org.apache.lucene.codecs.PostingsFormat.forName(PostingsFormat.java:112)
    at org.apache.lucene.codecs.lucene54.Lucene54Codec.<init>(Lucene54Codec.java:161)
    at org.apache.lucene.codecs.lucene54.Lucene54Codec.<init>(Lucene54Codec.java:81)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at java.lang.Class.newInstance(Class.java:442)
    at org.apache.lucene.util.NamedSPILoader.reload(NamedSPILoader.java:72)
    at org.apache.lucene.util.NamedSPILoader.<init>(NamedSPILoader.java:51)
    at org.apache.lucene.util.NamedSPILoader.<init>(NamedSPILoader.java:38)
    at org.apache.lucene.codecs.Codec$Holder.<clinit>(Codec.java:47)
    at org.apache.lucene.codecs.Codec.getDefault(Codec.java:140)
    at org.apache.lucene.index.LiveIndexWriterConfig.<init>(LiveIndexWriterConfig.java:120)
    at org.apache.lucene.index.IndexWriterConfig.<init>(IndexWriterConfig.java:140)
    at org.neo4j.kernel.api.impl.index.IndexWriterConfigs.standard(IndexWriterConfigs.java:69)
    at org.neo4j.kernel.api.impl.index.partition.WritableIndexPartitionFactory.createPartition(WritableIndexPartitionFactory.java:45)
    at org.neo4j.kernel.api.impl.index.AbstractLuceneIndex.open(AbstractLuceneIndex.java:98)
...

This does not make sense to me at all, because:

  1. I'm not calling Elasticsearch at all (I've got only a int i = 0; in my test).
  2. To make sure the org/apache/logging/log4j/Logger class is available, I instantiated a static logger in my test class and the class is found this time: final static org.apache.log4j.Logger logger = Logger.getLogger(ApplicationTests.class); The funny thing is that this static logger object is instantiated successfully before the exception is thrown in Elasticsearch's code!

Any idea why I'm facing this exception?

To demonstrate the issue, I've created this project on GitHub which you can clone. Try and run the test dummyTest and you'll see the exception.

Hi have found the answer here: https://discuss.elastic.co/t/issue-with-elastic-search-5-0-0-noclassdeffounderror-org-apache-logging-log4j-logger/64262/3

It seems that log4j 2.7 does not work with ElasticSearch 5.0.0. And you also need to add a dependency on the libraries slf4j-simple and log4j-to-slf4j along with log4j-api .

For me, it worked with:

  • log4j-api version 2.8.2
  • slf4j-simple version 1.7.25
  • log4j-to-slf4j version 2.8.2
  • elasticsearch version 5.4.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM