一尘不染

java.lang.NoClassDefFoundError:org / apache / logging / log4j / Logger

elasticsearch

我想在我正在使用的Elasticsearch中执行单元测试,Java-test-framework
我正在使用Elasticsearch-1.6.0 并参考以下链接以寻求帮助
https://www.elastic.co/guide/en/elasticsearch/reference/1.6/using-
elasticsearch-test-classes .html

https://github.com/elastic/elasticsearch/blob/master/core/src/test/java/org/elasticsearch/action/search/SearchRequestBuilderTests.java

这是代码

class CampaignESTest extends ESTestCase {

  def getCLient():MockTransportClient={
     val settings = Settings.builder()
                .put(Environment.PATH_HOME_SETTING.getKey(), Files.createTempDir().toString())
                .build();
     val client = new MockTransportClient(settings);  
     client
  }
 }

class CampaignTestSearch extends PlaySpec{
  val client=new CampaignESTest

val response = client.prepareSearch("dbtest")
      .setTypes(CAMPAIGN_COLLECTION_NAME)
      .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
      .addFields("uuid","campaignName","artworkID","activationDate","_source")
      .setQuery(query)
      .execute()
      .actionGet()
  }

我收到这个例外

  DeferredAbortedSuite:
[info] Exception encountered when attempting to run a suite with class name: org.scalatest.DeferredAbortedSuite *** ABORTED ***
[info]   java.lang.NoClassDefFoundError: org/apache/logging/log4j/Logger
[info]   at org.elasticsearch.common.logging.Loggers.getLogger(Loggers.java:101)
[info]   at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:92)
[info]   at org.elasticsearch.bootstrap.BootstrapForTesting.<clinit>(BootstrapForTesting.java:78)
[info]   at org.elasticsearch.test.ESTestCase.<clinit>(ESTestCase.java:138)
[info]   at testcontrollers.campaign.CampaignTestSerach.<init>(CampaignTestSerach.scala:40)
[info]   at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
[info]   at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
[info]   at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
[info]   at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
[info]   at java.lang.Class.newInstance(Class.java:442)
[info]   ...
[info]   Cause: java.lang.ClassNotFoundException: org.apache.logging.log4j.Logger
[info]   at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
[info]   at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
[info]   at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
[info]   at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
[info]   at org.elasticsearch.common.logging.Loggers.getLogger(Loggers.java:101)
[info]   at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:92)
[info]   at org.elasticsearch.bootstrap.BootstrapForTesting.<clinit>(BootstrapForTesting.java:78)
[info]   at org.elasticsearch.test.ESTestCase.<clinit>(ESTestCase.java:138)
[info]   at testcontrollers.campaign.CampaignTestSerach.<init>(CampaignTestSerach.scala:40)
[info]   at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
[info]   ...
[error] Uncaught exception when running testcontrollers.campaign.CampaignTestSerach: java.lang.NoClassDefFoundError: org/apache/logging/log4j/Logger

在这条线上得到一个例外

val client=new CampaignESTest

在班上 CampaignTestSearch

这是build.sbt文件中的依赖项

   "org.slf4j" % "slf4j-api" % "1.6.4",
                "org.elasticsearch" % "elasticsearch" % "1.6.0",
                "org.elasticsearch.test" % "framework" % "5.0.0" % "test",
                "org.apache.lucene" % "lucene-test-framework" % "4.10.4" % "test",
                "com.carrotsearch.randomizedtesting" % "randomizedtesting-runner" % "1.6.0" % "test",
                "org.apache.lucene" % "lucene-codecs" % "4.10.4" % "test",
                "org.apache.logging.log4j" % "log4j-core" % "2.8.2" ,
"org.apache.lucene" % "lucene-expressions" % "4.10.4",

我怀疑引起异常的原因可能是由于某些版本的依赖关系,任何人都可以帮助提供正确的库依赖关系版本以使其正常工作,或者我还缺少其他东西

编辑 从“ org.apache.logging.log4j”中删除的“测试”%“ log4j-core”%“ 2.8.2”
更新build.sbt

"org.slf4j" % "slf4j-api" % "1.6.4",
                "org.elasticsearch" % "elasticsearch" % "1.6.0",
                "org.elasticsearch.test" % "framework" % "5.0.0" % "test",
                "org.apache.lucene" % "lucene-test-framework" % "4.10.4" % "test",
                "com.carrotsearch.randomizedtesting" % "randomizedtesting-runner" % "1.6.0" % "test",
                "org.apache.lucene" % "lucene-codecs" % "4.10.4" % "test",
                "org.apache.logging.log4j" % "log4j-core" % "2.8.2",
                "org.apache.logging.log4j" % "log4j-slf4j-impl" % "2.8.2" ,
                "org.apache.lucene" % "lucene-expressions" % "4.10.4",

阅读 511

收藏
2020-06-22

共1个答案

一尘不染

要解决您的问题,请添加以下缺少的依赖项:

  • org.apache.logging.log4j:log4j-api:2.8.2

另外,与问题没有直接关系,请添加以下依赖项,以便您自己对slf4j api的调用也将定向到log4j2实现:

  • org.apache.logging.log4j:log4j-slf4j-impl:2.8.2

确保在类路径上没有其他日志实现。

注意:我提出的“额外”建议是基于您想将所有日志调用重定向到log4j的,无论您是调用slf4j
api还是直接调用log4j。如果不是,请说明您要使用哪种日志实现,因为那样的话,OP列出的依赖关系也会丢失。

2020-06-22