引起:java.lang.NoClassDefFoundError:org/apache/log4j/Logger

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20909446/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:15:09  来源:igfitidea点击:

Caused By: java.lang.NoClassDefFoundError: org/apache/log4j/Logger

javaloggingclasspathclassloaderintuit-partner-platform

提问by lovebeer84

I've got an interesting problem in which the org.apache.log4j.Logger class is not found during runtime. I'm trying to get authorized and that is where it's failing:

我有一个有趣的问题,其中在运行时找不到 org.apache.log4j.Logger 类。我正在尝试获得授权,这就是失败的地方:

OAuthAuthorizer oauthAuthorizer = new OAuthAuthorizer(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, SAML_PROVIDER_ID, userId);

OAuthAuthorizer oauthAuthorizer = new OAuthAuthorizer(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, SAML_PROVIDER_ID, userId);

I'm using JDeveloper 11.1.1.6. Here is what I know:

我正在使用 JDeveloper 11.1.1.6。这是我所知道的:

  1. I've looked in my UI.war/WEB-INF/lib directory and I see the log4j-1.2.17.jar there.

  2. The class complaining about it is org.opensaml.xml.XMLConfigurator

    Caused by: java.lang.NoClassDefFoundError: org/apache/log4j/Logger
        at org.opensaml.xml.XMLConfigurator.<clinit>(XMLConfigurator.java:60)
        at org.opensaml.DefaultBootstrap.initializeXMLTooling(DefaultBootstrap.java:195)
        at org.opensaml.DefaultBootstrap.bootstrap(DefaultBootstrap.java:91)
        at com.intuit.ipp.aggcat.util.SAML2AssertionGenerator.getSAMLBuilder(SAML2AssertionGenerator.java:156)
        at com.intuit.ipp.aggcat.util.SAML2AssertionGenerator.createSubject(SAML2AssertionGenerator.java:187)
        at com.intuit.ipp.aggcat.util.SAML2AssertionGenerator.buildAssertion(SAML2AssertionGenerator.java:114)
        at com.intuit.ipp.aggcat.util.SAML2AssertionGenerator.generateSignedAssertion(SAML2AssertionGenerator.java:83)
        at com.intuit.ipp.aggcat.util.SamlUtil.createSignedSAMLPayload(SamlUtil.java:156)
        at com.intuit.ipp.aggcat.util.OAuthUtil.getOAuthTokens(OAuthUtil.java:60)
        at com.intuit.ipp.aggcat.core.OAuthAuthorizer.<init>(OAuthAuthorizer.java:85)
        at com.incomemax.view.intuit.WebUtil.getAggCatService(WebUtil.java:91)
    
    Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
        at java.net.URLClassLoader.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:305)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:246)
        ... 64 more
    
  3. I decomplied XMLConfigurator and oddly it doesn't import org.apache.log4j.Logger It uses org.slf4j.Logger which is also in my jars directory (slf4j-api-1.7.5.jar). Also interesting is that line 60 (see stack trace) is a blank line in my decompile.

  4. Of course if I add Logger.xxxxx during design time, it finds it just fine.

  5. I'm using the code/jars directly from the sample java code, but imported into my existing application.

  1. 我查看了我的 UI.war/WEB-INF/lib 目录,我在那里看到了 log4j-1.2.17.jar。

  2. 抱怨它的类是 org.opensaml.xml.XMLConfigurator

    Caused by: java.lang.NoClassDefFoundError: org/apache/log4j/Logger
        at org.opensaml.xml.XMLConfigurator.<clinit>(XMLConfigurator.java:60)
        at org.opensaml.DefaultBootstrap.initializeXMLTooling(DefaultBootstrap.java:195)
        at org.opensaml.DefaultBootstrap.bootstrap(DefaultBootstrap.java:91)
        at com.intuit.ipp.aggcat.util.SAML2AssertionGenerator.getSAMLBuilder(SAML2AssertionGenerator.java:156)
        at com.intuit.ipp.aggcat.util.SAML2AssertionGenerator.createSubject(SAML2AssertionGenerator.java:187)
        at com.intuit.ipp.aggcat.util.SAML2AssertionGenerator.buildAssertion(SAML2AssertionGenerator.java:114)
        at com.intuit.ipp.aggcat.util.SAML2AssertionGenerator.generateSignedAssertion(SAML2AssertionGenerator.java:83)
        at com.intuit.ipp.aggcat.util.SamlUtil.createSignedSAMLPayload(SamlUtil.java:156)
        at com.intuit.ipp.aggcat.util.OAuthUtil.getOAuthTokens(OAuthUtil.java:60)
        at com.intuit.ipp.aggcat.core.OAuthAuthorizer.<init>(OAuthAuthorizer.java:85)
        at com.incomemax.view.intuit.WebUtil.getAggCatService(WebUtil.java:91)
    
    Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
        at java.net.URLClassLoader.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:305)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:246)
        ... 64 more
    
  3. 我反编译了 XMLConfigurator,奇怪的是它没有导入 org.apache.log4j.Logger 它使用 org.slf4j.Logger,它也在我的 jars 目录(slf4j-api-1.7.5.jar)中。同样有趣的是,第 60 行(参见堆栈跟踪)在我的反编译中是一个空行。

  4. 当然,如果我在设计时添加 Logger.xxxxx ,它发现它就好了。

  5. 我直接使用示例 java 代码中的代码/罐子,但导入到我现有的应用程序中。

I've been scouring the web for answers and I believe I've checked all the areas I can think of. I also referenced this very good page: http://myarch.com/classnotfound/

我一直在网上搜索答案,我相信我已经检查了我能想到的所有领域。我还参考了这个非常好的页面:http: //myarch.com/classnotfound/

Given authorization is step 1 in using the Intuit Developer API, I'm kinda stuck.

鉴于授权是使用 Intuit Developer API 的第 1 步,我有点卡住了。

Adding output from @jhadesdev suggestion:

添加来自@jhadesdev 建议的输出:

All versions of log4j Logger:

log4j 记录器的所有版本:

  • zip:C:/Users/Chris/AppData/Roaming/JDeveloper/system11.1.1.6.38.61.92/DefaultDomain/servers/DefaultServer/tmp/_WL_user/j2ee-app/lt5l71/war/WEB-INF/lib/log4j-1.2.17.jar!/org/apache/log4j/Logger.class
  • zip:C:/Users/Chris/AppData/Roaming/JDeveloper/system11.1.1.6.38.61.92/DefaultDomain/servers/DefaultServer/tmp/_WL_user/j2ee-app/lt5l71/war/WEB-INF/lib/log4j-1.2 .17.jar!/org/apache/log4j/Logger.class

All versions of log4j visible from the classloader of the OAuthAuthorizer class:

从 OAuthAuthorizer 类的类加载器中可以看到 log4j 的所有版本:

  • zip:C:/Users/Chris/AppData/Roaming/JDeveloper/system11.1.1.6.38.61.92/DefaultDomain/servers/DefaultServer/tmp/_WL_user/j2ee-app/lt5l71/war/WEB-INF/lib/log4j-1.2.17.jar!/org/apache/log4j/Logger.class
  • zip:C:/Users/Chris/AppData/Roaming/JDeveloper/system11.1.1.6.38.61.92/DefaultDomain/servers/DefaultServer/tmp/_WL_user/j2ee-app/lt5l71/war/WEB-INF/lib/log4j-1.2 .17.jar!/org/apache/log4j/Logger.class

All versions of XMLConfigurator:

XMLConfigurator 的所有版本:

  • jar:file:/C:/Oracle/Middleware11116/modules/com.bea.core.bea.opensaml2_1.0.0.0_6-1-0-0.jar!/org/opensaml/xml/XMLConfigurator.class

  • zip:C:/Users/Chris/AppData/Roaming/JDeveloper/system11.1.1.6.38.61.92/DefaultDomain/servers/DefaultServer/tmp/_WL_user/j2ee-app/lt5l71/war/WEB-INF/lib/ipp-java-aggcat-v1-devkit-1.0.2.jar!/org/opensaml/xml/XMLConfigurator.class

  • zip:C:/Users/Chris/AppData/Roaming/JDeveloper/system11.1.1.6.38.61.92/DefaultDomain/servers/DefaultServer/tmp/_WL_user/j2ee-app/lt5l71/war/WEB-INF/lib/xmltooling-1.3.1.jar!/org/opensaml/xml/XMLConfigurator.class

  • jar:file:/C:/Oracle/Middleware11116/modules/com.bea.core.bea.opensaml2_1.0.0.0_6-1-0-0.jar!/org/opensaml/xml/XMLConfigurator.class

  • zip:C:/Users/Chris/AppData/Roaming/JDeveloper/system11.1.1.6.38.61.92/DefaultDomain/servers/DefaultServer/tmp/_WL_user/j2ee-app/lt5l71/war/WEB-INF/lib/ipp-java -aggcat-v1-devkit-1.0.2.jar!/org/opensaml/xml/XMLConfigurator.class

  • zip:C:/Users/Chris/AppData/Roaming/JDeveloper/system11.1.1.6.38.61.92/DefaultDomain/servers/DefaultServer/tmp/_WL_user/j2ee-app/lt5l71/war/WEB-INF/lib/xmltooling-1.3 .1.jar!/org/opensaml/xml/XMLConfigurator.class

All versions of XMLConfigurator visible from the class loader of the OAuthAuthorizer class:

从 OAuthAuthorizer 类的类加载器中可以看到 XMLConfigurator 的所有版本:

  • jar:file:/C:/Oracle/Middleware11116/modules/com.bea.core.bea.opensaml2_1.0.0.0_6-1-0-0.jar!/org/opensaml/xml/XMLConfigurator.class

  • zip:C:/Users/Chris/AppData/Roaming/JDeveloper/system11.1.1.6.38.61.92/DefaultDomain/servers/DefaultServer/tmp/_WL_user/j2ee-app/lt5l71/war/WEB-INF/lib/ipp-java-aggcat-v1-devkit-1.0.2.jar!/org/opensaml/xml/XMLConfigurator.class

  • zip:C:/Users/Chris/AppData/Roaming/JDeveloper/system11.1.1.6.38.61.92/DefaultDomain/servers/DefaultServer/tmp/_WL_user/j2ee-app/lt5l71/war/WEB-INF/lib/xmltooling-1.3.1.jar!/org/opensaml/xml/XMLConfigurator.class

  • jar:file:/C:/Oracle/Middleware11116/modules/com.bea.core.bea.opensaml2_1.0.0.0_6-1-0-0.jar!/org/opensaml/xml/XMLConfigurator.class

  • zip:C:/Users/Chris/AppData/Roaming/JDeveloper/system11.1.1.6.38.61.92/DefaultDomain/servers/DefaultServer/tmp/_WL_user/j2ee-app/lt5l71/war/WEB-INF/lib/ipp-java -aggcat-v1-devkit-1.0.2.jar!/org/opensaml/xml/XMLConfigurator.class

  • zip:C:/Users/Chris/AppData/Roaming/JDeveloper/system11.1.1.6.38.61.92/DefaultDomain/servers/DefaultServer/tmp/_WL_user/j2ee-app/lt5l71/war/WEB-INF/lib/xmltooling-1.3 .1.jar!/org/opensaml/xml/XMLConfigurator.class

I'm still working on interpreting the results.

我仍在努力解释结果。

采纳答案by lovebeer84

With the suggestions @jhadesdev and the explanations from others, I've found the issue here.

根据@jhadesdev 的建议和其他人的解释,我在这里找到了问题。

After adding the code to see what was visible to the various class loaders I found this:

添加代码以查看各种类加载器可见的内容后,我发现了这一点:

All versions of log4j Logger: 
  zip:<snip>war/WEB-INF/lib/log4j-1.2.17.jar!/org/apache/log4j/Logger.class

All versions of log4j visible from the classloader of the OAuthAuthorizer class: 
  zip:<snip>war/WEB-INF/lib/log4j-1.2.17.jar!/org/apache/log4j/Logger.class

All versions of XMLConfigurator: 
  jar:<snip>com.bea.core.bea.opensaml2_1.0.0.0_6-1-0-0.jar!/org/opensaml/xml/XMLConfigurator.class
  zip:<snip>war/WEB-INF/lib/ipp-java-aggcat-v1-devkit-1.0.2.jar!/org/opensaml/xml/XMLConfigurator.class
  zip:<snip>war/WEB-INF/lib/xmltooling-1.3.1.jar!/org/opensaml/xml/XMLConfigurator.class

All versions of XMLConfigurator visible from the classloader of the OAuthAuthorizer class: 
  jar:<snip>com.bea.core.bea.opensaml2_1.0.0.0_6-1-0-0.jar!/org/opensaml/xml/XMLConfigurator.class
  zip:<snip>war/WEB-INF/lib/ipp-java-aggcat-v1-devkit-1.0.2.jar!/org/opensaml/xml/XMLConfigurator.class
  zip:<snip>war/WEB-INF/lib/xmltooling-1.3.1.jar!/org/opensaml/xml/XMLConfigurator.class

I noticed that another version of XMLConfigurator was possibly getting picked up. I decompiled that class and found this at line 60 (where the error was in the original stack trace) private static final Logger log = Logger.getLogger(XMLConfigurator.class);and that class was importing from org.apache.log4j.Logger!

我注意到另一个版本的 XMLConfigurator 可能会被采用。我反编译了那个类并在第 60 行(错误在原始堆栈跟踪中)发现了这个,private static final Logger log = Logger.getLogger(XMLConfigurator.class);并且那个类是从org.apache.log4j.Logger!

So it was this class that was being loaded and used. My fix was to rename the jar file that contained this file as I can't find where I explicitly or indirectly load it. Which may pose a problem when I actually deploy.

所以正是这个类被加载和使用。我的解决方法是重命名包含此文件的 jar 文件,因为我找不到明确或间接加载它的位置。当我实际部署时,这可能会带来问题。

Thanks for all help and the much needed lesson on class loading.

感谢所有的帮助和关于类加载的急需的课程。

回答by Premraj

During runtime your application is unable to find the jar.

在运行时,您的应用程序无法找到 jar。

Taken from this answerby Jared:

来自这个答案贾里德

It is important to keep two different exceptions straight in our head in this case:

  1. java.lang.ClassNotFoundExceptionThis an Exception, it indicates that the class was not found on the classpath. This indicates that we were trying to load the class definition, and the class did not exist on the classpath.

  2. java.lang.NoClassDefFoundErrorThis is Error, it indicates that the JVM looked in its internal class definition data structure for the definition of a class and did not find it. This is different than saying that it could not be loaded from the classpath. Usually this indicates that we previously attempted to load a class from the classpath, but it failed for some reason - now we're trying again, but we're not even going to try to load it, because we failed loading it earlier. The earlier failure could be a ClassNotFoundException or an ExceptionInInitializerError (indicating a failure in the static initialization block) or any number of other problems. The point is, a NoClassDefFoundError is not necessarily a classpath problem.

在这种情况下,我们必须牢记两个不同的例外:

  1. java.lang.ClassNotFoundException这是一个Exception,表示在类路径上找不到该类。这表明我们正在尝试加载类定义,并且类路径上不存在该类。

  2. java.lang.NoClassDefFoundError这是Error,说明JVM在其内部的类定义数据结构中查找类的定义,没有找到。这与说它无法从类路径加载不同。通常这表明我们之前尝试从类路径加载一个类,但由于某种原因它失败了 - 现在我们再次尝试,但我们甚至不会尝试加载它,因为我们之前加载它失败了。较早的失败可能是 ClassNotFoundException 或 ExceptionInInitializerError(指示静态初始化块中的失败)或任何数量的其他问题。关键是, NoClassDefFoundError 不一定是类路径问题。

for similarities and differences

异同

回答by Dharmraj

java.lang.ClassNotFoundException is indicate that class is not found in class path. it could be the version of log4j is not compatible. check for different log4j version.

java.lang.ClassNotFoundException 表示在类路径中找不到类。可能是 log4j 的版本不兼容。检查不同的 log4j 版本。

回答by Manas Mukherjee

You can use the following maven dependency in your pom file. Otherwise, you can download the following two jars from net and add it to your build path.

您可以在 pom 文件中使用以下 maven 依赖项。否则,您可以从网上下载以下两个 jar 并将其添加到您的构建路径中。

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.6.4</version>
</dependency>

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.6.4</version>
</dependency>

This is copied from my working project. First make sure it is working in your project. Then you can change the versions to use any other(versions) compatible jars.

这是从我的工作项目中复制的。首先确保它在您的项目中工作。然后您可以更改版本以使用任何其他(版本)兼容的 jar。

For AggCat, you can refer the POM file of the sample java application.

对于 AggCat,您可以参考示例 java 应用程序的 POM 文件。

https://github.com/IntuitDeveloperRelations/IPP_Sample_Code/blob/master/CustomerAccountData/Java/AggCatSampleApplication/pom.xml

https://github.com/IntuitDeveloperRelations/IPP_Sample_Code/blob/master/CustomerAccountData/Java/AggCatSampleApplication/pom.xml

Thanks

谢谢

回答by Angular University

Based on the stacktrace, an intuit class com.intuit.ipp.aggcat.util.SAML2AssertionGenerator needs a saml jar on the classpath.

基于堆栈跟踪,一个直觉类 com.intuit.ipp.aggcat.util.SAML2AssertionGenerator 在类路径上需要一个 saml jar。

A saml class org.opensaml.xml.XMLConfigurator needs on it's turn log4j, which is inside the WAR but cannot find it.

一个 saml 类 org.opensaml.xml.XMLConfigurator 需要它轮到 log4j,它在 WAR 中但找不到它。

One explanation for this is that the class XMLConfigurator that needs log4j was found not inside the WAR but on a downstream classloader. could a saml jar be missing from the WAR?

对此的一种解释是需要 log4j 的类 XMLConfigurator 不是在 WAR 中找到的,而是在下游类加载器中找到的。WAR 中可能会缺少一个 saml jar 吗?

The class XMLConfigurator that needs log4j cannot find it at the level of the classloader that loaded it, and the log4j version on the WAR is not visible on that particular classloader.

需要 log4j 的类 XMLConfigurator 在加载它的类加载器级别找不到它,并且 WAR 上的 log4j 版本在该特定类加载器上不可见。

In order to troubleshoot this, a way is to add this before the oauth call:

为了解决这个问题,一种方法是在 oauth 调用之前添加它:

System.out.println("all versions of log4j Logger: " + getClass().getClassLoader().getResources("org/apache/log4j/Logger.class") );

System.out.println("all versions of XMLConfigurator: " + getClass().getClassLoader().getResources("org/opensaml/xml/XMLConfigurator.class") );

System.out.println("all versions of XMLConfigurator visible from the classloader of the OAuthAuthorizer class: " + OAuthAuthorizer.class.getClassLoader().getResources("org/opensaml/xml/XMLConfigurator.class") );

System.out.println("all versions of log4j visible from the classloader of the OAuthAuthorizer class: " + OAuthAuthorizer.class.getClassloader().getResources("org/apache/log4j/Logger.class") );

Also if you are using Java 7, have a look at jHades, it's a tool I made to help troubleshooting these type of problems.

另外,如果您使用的是 Java 7,请查看jHades,这是我用来帮助解决此类问题的工具。

In order to see what is going on, could you post the results of the classpath queries above, for which container is this happening, tomcat, jetty? It would be better to put the full stacktrace with all the caused by's in pastebin, just in case.

为了看看发生了什么,您能否发布上面类路径查询的结果,这是针对哪个容器发生的,tomcat,jetty?最好将完整的堆栈跟踪和所有由 引起的放在 pastebin 中,以防万一。

回答by Denis Tulskiy

Had the same problem, it was indeed caused by weblogic stupidly using its own opensaml implementation. To solve it, you have to tell it to load classes from WEB-INF/libfor this package in weblogic.xml:

有同样的问题,确实是由weblogic愚蠢地使用自己的opensaml实现引起的。要解决它,您必须告诉它从WEB-INF/lib以下位置为此包加载类weblogic.xml

    <prefer-application-packages>
        <package-name>org.opensaml.*</package-name>
    </prefer-application-packages>

maybe <prefer-web-inf-classes>true</prefer-web-inf-classes>would work too.

也许<prefer-web-inf-classes>true</prefer-web-inf-classes>也会起作用。

回答by perezmirabile

Check in Deployment Assembly,

签入部署程序集,

I have the same error, when i generate the war file with the "maven clean install" way and deploy manualy, it works fine, but when i use the runtime enviroment (eclipse) the problems come.

我有同样的错误,当我使用“maven clean install”方式生成war文件并手动部署时,它工作正常,但是当我使用运行时环境(eclipse)时,问题就来了。

The solution for me (for eclipse IDE) go to: "proyect properties" --> "Deployment Assembly" --> "Add" --> "the jar you need", in my case java "build path entries". Maybe can help a litle!

我的解决方案(对于 Eclipse IDE)转到:“项目属性”-->“部署程序集”-->“添加”-->“您需要的 jar”,在我的情况下是 java“构建路径条目”。也许能帮上一点忙!

回答by JavaSheriff

I had the same issue, for me this fixed the issue:
right click on the project ->maven -> update project

我有同样的问题,对我来说这解决了这个问题:
右键单击项目 -> maven -> 更新项目

maven ->update project

maven ->更新项目