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

时间:2017-08-19 06:33:48

标签: java scala unit-testing 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",

我怀疑异常的原因可能是由于某些版本的依赖项,任何人都可以帮助使用正确的库依赖版本来使其工作或者是否还有其他我缺少的东西

修改 删除&#34;测试&#34;来自&#34; org.apache.logging.log4j&#34; %&#34; log4j-core&#34; %&#34; 2.8.2&#34; 更新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",

2 个答案:

答案 0 :(得分:4)

修复您的问题,添加以下缺少的依赖项:

  • 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列出的依赖性也缺少了。

答案 1 :(得分:-1)

AnimatedSource错误表明,JVM无法在项目或应用程序CLASSPATH中找到org.apache.log4j.Logger类。可能是错误的原因是缺少log4j.jar文件到你的项目或类路径。所以你需要使用以下依赖:

Caused By: Cause: java.lang.ClassNotFoundException: org.apache.logging.log4j.Logger