java.lang.NoClassDefFoundError:org / apache / log4j / Priority

时间:2021-10-22 21:47:57

I am following a custom logging tool to implement logging functionality for emails using log4j.

我正在关注自定义日志记录工具,以使用log4j实现电子邮件的日志记录功能。

I have added the dependency for the log4j:1.2.17 as well as the jar to the class path. I am trying to run a Test using arquillian. But every time I am getting the same error.

我已经将log4j:1.2.17的依赖项以及jar添加到类路径中。我正在尝试使用arquillian进行测试。但每次我都得到同样的错误。

I know its coming up as in I am using the Level instead of Priority which is now deprecated. I have added the correct jar and imports as well but still the error prevails when I run the Test.

我知道它会出现,因为我正在使用Level而不是Priority,现在已经弃用了。我已经添加了正确的jar和导入,但是当我运行测试时仍然存在错误。

I have referred to these links as well but nothing works for me.

我也提到过这些链接,但对我来说没什么用。

What's causing this Maven/JBehave error?

导致Maven / JBehave错误的原因是什么?

https://community.oracle.com/thread/969941?start=0

https://community.oracle.com/thread/969941?start=0

Does anybody know how to solve this error?

有谁知道如何解决这个错误?

This is a snippet from my EmailTest.java class:

这是我的EmailTest.java类的片段:

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.*;
import org.junit.runner.RunWith;
import org.apache.log4j.Level;
import org.apache.log4j.spi.LoggingEvent;
import org.apache.log4j.spi.RootLogger;
import javax.inject.Inject;

       public class EmailTest{
       @Inject
       private Email email;

       @Deployment //(testable = true)
       public static JavaArchive createTestArchive(){

        return ShrinkWrap.create(JavaArchive.class, "test.jar")
                .addClasses(ErrorEmailThrottle.class)
                .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");}

        @Test
        public void noFatalError(){            
        Assert.assertEquals(email.isTriggeringEvent(createLogEvent(Level.FATAL, System.currentTimeMillis())), true);
        Assert.assertEquals(email.isInThrottleMode(), false);
                    }
        .....

        private LoggingEvent createLogEvent(Level level, long time) {
                return new LoggingEvent("Email", new RootLogger(Level.INFO) , time, level, "Email message", null);
              }
        }

Here is my pom.xml dependency:

这是我的pom.xml依赖项:

<dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
  <version>1.2.17</version>
   <!--<scope>test</scope>-->
   <exclusions>
        <exclusion>
          <groupId>com.sun.jdmk</groupId>
          <artifactId>jmxtools</artifactId>
        </exclusion>
        <exclusion>
           <groupId>com.sun.jmx</groupId>
           <artifactId>jmxri</artifactId>
        </exclusion>
    </exclusions>
 </dependency>

Getting this error:

得到此错误:

java.lang.NoClassDefFoundError: org/apache/log4j/Priority
    at java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.lang.Class.privateGetDeclaredConstructors(Class.java:2671)
    at java.lang.Class.getConstructors(Class.java:1651)
    at org.junit.runners.model.TestClass.<init>(TestClass.java:39)
    at org.junit.runners.ParentRunner.<init>(ParentRunner.java:75)
    at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:57)
    at org.jboss.arquillian.junit.Arquillian.<init>(Arquillian.java:60)
    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:422)
    at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:29)
    at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:21)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:26)
    at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:31)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:138)
    at org.jboss.arquillian.junit.container.JUnitTestRunner.execute(JUnitTestRunner.java:66)
    at org.jboss.arquillian.protocol.servlet.runner.ServletTestRunner.executeTest(ServletTestRunner.java:170)
    at org.jboss.arquillian.protocol.servlet.runner.ServletTestRunner.execute(ServletTestRunner.java:135)
    at org.jboss.arquillian.protocol.servlet.runner.ServletTestRunner.doGet(ServletTestRunner.java:98)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
    at java.lang.Thread.run(Thread.java:745)


Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Priority
    at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1509)
    at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1359)
    ... 49 more

1 个解决方案

#1


3  

I have just checked Log4j 1.2.17 source code, and org.apache.log4j.Priority is still there.

我刚刚检查了Log4j 1.2.17源代码,而org.apache.log4j.Priority仍然存在。

Therefore the problem is probably because your dependency is not properly added.

因此问题可能是因为没有正确添加依赖项。

There may be several possibilities:

可能有以下几种可能性:

  1. You have put that <dependency> block under <dependencyManagement> instead of <dependencies>
  2. 您已将 块放在 下而不是
  3. There are other dependency declared which Maven decided to take. It should be easily verifiable by mvn dependency:tree at the project. You can see which version of log4j is taken
  4. Maven决定采用其他依赖声明。它应该很容易通过mvn依赖:项目中的树来验证。您可以看到采用了哪个版本的log4j
  5. You file is damaged or empty or whatever. Check the content of log4j:log4j:1.2.17 in your local repository etc.
  6. 您的文件已损坏或空或其他。检查本地存储库中log4j:log4j:1.2.17的内容。

#1


3  

I have just checked Log4j 1.2.17 source code, and org.apache.log4j.Priority is still there.

我刚刚检查了Log4j 1.2.17源代码,而org.apache.log4j.Priority仍然存在。

Therefore the problem is probably because your dependency is not properly added.

因此问题可能是因为没有正确添加依赖项。

There may be several possibilities:

可能有以下几种可能性:

  1. You have put that <dependency> block under <dependencyManagement> instead of <dependencies>
  2. 您已将 块放在 下而不是
  3. There are other dependency declared which Maven decided to take. It should be easily verifiable by mvn dependency:tree at the project. You can see which version of log4j is taken
  4. Maven决定采用其他依赖声明。它应该很容易通过mvn依赖:项目中的树来验证。您可以看到采用了哪个版本的log4j
  5. You file is damaged or empty or whatever. Check the content of log4j:log4j:1.2.17 in your local repository etc.
  6. 您的文件已损坏或空或其他。检查本地存储库中log4j:log4j:1.2.17的内容。