编程语言
首页 > 编程语言> > java.lang.NoClassDefFoundError:org / apache / log4j / Priority

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

作者:互联网

我正在跟踪一个自定义日志记录工具,以使用log4j实现电子邮件的日志记录功能.

我已将log4j:1.2.17的依赖项以及jar添加到类路径.我正在尝试使用Arquillian进行测试.但是每次我遇到同样的错误.

我知道它即将出现,就像我使用的是Level而不是现在不推荐使用的Priority一样.我也添加了正确的jar和导入,但是运行Test时仍然会出现错误.

我也提到了这些链接,但对我来说没有用.

What’s causing this Maven/JBehave error?

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

有人知道如何解决此错误吗?

这是我的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);
              }
        }

这是我的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>

收到此错误:

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

解决方法:

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

因此,问题可能出在您的依赖项未正确添加上.

可能有几种可能性:

>您已将< dependency> < dependencyManagement>下的阻止而不是< dependencies>
>还声明了Maven决定采用的其他依赖项.它应该可以在项目中通过mvndependency:tree轻松验证.您可以看到采用了哪个版本的log4j
>您的文件已损坏或为空或任何其他内容.检查本地存储库中log4j:log4j:1.2.17的内容.

标签:maven,log4j,jboss-arquillian,glassfish-3,java
来源: https://codeday.me/bug/20191027/1946113.html