scala java.lang.NoClassDefFoundError: org/apache/logging/log4j/Logger
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45768480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.lang.NoClassDefFoundError: org/apache/logging/log4j/Logger
提问by swaheed
I want to perform unit testing in Elasticsearch for that I am using Java-test-framework
I am using Elasticsearch-1.6.0and referring to these link for help
https://www.elastic.co/guide/en/elasticsearch/reference/1.6/using-elasticsearch-test-classes.htmlhttps://github.com/elastic/elasticsearch/blob/master/core/src/test/java/org/elasticsearch/action/search/SearchRequestBuilderTests.java
我想在 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
here is the code
这是代码
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()
}
I am getting this exception
我收到此异常
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
getting an exception on this line
在这条线上得到一个例外
val client=new CampaignESTest
in class CampaignTestSearch
在班上 CampaignTestSearch
here are the dependencies in build.sbtfile
这是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",
i suspect the cause of the exception maybe due to some versions of the dependencies can anyone please help with the correct library dependency version to make it to work or is there something else which i am missing
我怀疑异常的原因可能是由于某些版本的依赖项,任何人都可以帮助使用正确的库依赖项版本以使其正常工作,或者还有其他我遗漏的东西
Editremoved "test" from "org.apache.logging.log4j" % "log4j-core" % "2.8.2"
Update build.sbt
编辑从“org.apache.logging.log4j”中删除“test”%“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",
回答by albert_nil
to fix your issue, add following missing dependency:
要解决您的问题,请添加以下缺少的依赖项:
- org.apache.logging.log4j:log4j-api:2.8.2
- org.apache.logging.log4j:log4j-api:2.8.2
aside, not directly related to issue, add following dependency so that your own calls to slf4j api get directed to log4j2 implementation also:
除了与问题没有直接关系,添加以下依赖项,以便您自己对 slf4j api 的调用也被定向到 log4j2 实现:
- org.apache.logging.log4j:log4j-slf4j-impl:2.8.2
- org.apache.logging.log4j:log4j-slf4j-impl:2.8.2
ensure no other log implementation is present on classpath.
确保类路径上不存在其他日志实现。
note: the "extra" suggestion i made is based in that you want to redirect all your log calls to log4j, no matter if you are calling slf4j api or directly log4j. If not, please state which log implementatjon you would like, since then the OP listed depenedencies is also missing something.
注意:我提出的“额外”建议是基于您希望将所有日志调用重定向到 log4j,无论您是调用 slf4j api 还是直接调用 log4j。如果没有,请说明您想要哪个日志实现,因为那时 OP 列出的依赖项也缺少一些东西。
回答by Anshul Sharma
Caused By: Cause: java.lang.ClassNotFoundException: org.apache.logging.log4j.Loggererror indicates that, JVM is not able to found org.apache.log4j.Logger class in your project or application CLASSPATH. may be the reason of error is missing log4j.jar file into you project or class path. so you need to use below dependency:
Caused By: Cause: java.lang.ClassNotFoundException: org.apache.logging.log4j.Logger错误表示,JVM 无法在您的项目或应用程序 CLASSPATH 中找到 org.apache.log4j.Logger 类。可能是您的项目或类路径中缺少 log4j.jar 文件的错误原因。所以你需要使用以下依赖项:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>

