apache 如何绕过这个无效的类加载器层次结构?

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

How can I get around this invalid classloader hierarchy?

javaapacheloggingclassloader

提问by stu

I run iPlanet's Java application server, something in it is loading commons-logging-1.0.4.jar.

我运行 iPlanet 的 Java 应用程序服务器,其中的内容正在加载commons-logging-1.0.4.jar.

That's fine until one of my applications calls AuthSSLProtocolSocketFactorywhich is another apache library that also uses commons-logging.

这很好,直到我的一个应用程序调用AuthSSLProtocolSocketFactory它是另一个也使用commons-logging.

I put the jar on the jvm classpath and get this error:

我将 jar 放在 jvm 类路径上并收到此错误:

Invalid class loader hierarchy. You have more than one version of 'org.apache.commons.logging.Log' visible, which is not allowed. (Caused by org.apache.commons.logging.LogConfigurationException: Invalid class loader hierarchy....

Invalid class loader hierarchy. You have more than one version of 'org.apache.commons.logging.Log' visible, which is not allowed. (Caused by org.apache.commons.logging.LogConfigurationException: Invalid class loader hierarchy....

It seems that commons-loggerdoesn't like having two instances of itself loaded in different classloaders. I assume the application server has its own classloader that's loading it the first time (Although I can't find any appserver configuration that mentions it) so when my application goes to load it a second time it throws that exception.

似乎commons-logger不喜欢在不同的类加载器中加载两个自身的实例。我假设应用程序服务器有自己的类加载器,第一次加载它(虽然我找不到任何提到它的应用程序服务器配置),所以当我的应用程序第二次加载它时,它会抛出该异常。

I can't change the web server, and I can't change the apache library. Suggestions?

我无法更改 Web 服务器,也无法更改 apache 库。建议?

采纳答案by AngerClown

Are you putting commons logging in your classpath explicitly? You said jvm classpath, so I assume you are specifying it on the command line when you start iPlanet. That's not the recommended way to load jars in J2EE apps.

您是否明确将公共日志记录在您的类路径中?您说的是 jvm 类路径,所以我假设您在启动 iPlanet 时在命令行中指定了它。这不是在 J2EE 应用程序中加载 jar 的推荐方法。

The easiest thing is to just let the Apache library use the commons logging jar that comes with iPlanet. Don't put commons-logging.jar in your WEB-INF/lib dir or in any classpath setting and the iPlanet one should be picked up automatically.

最简单的方法是让 Apache 库使用 iPlanet 附带的公共日志记录 jar。不要将 commons-logging.jar 放在您的 WEB-INF/lib 目录或任何类路径设置中,iPlanet 应该会被自动选取。

回答by grayger

Take a look at SLF4J.

看看SLF4J

Additionally, http://www.qos.ch/logging/classloader.jsp will help.

此外,http://www.qos.ch/logging/classloader.jsp会有所帮助。

回答by Robin

Not familiar with iplanet, but in WebSphere you can set your applications classloading policy to be PARENT_LAST. This will then load everything in your applications classloader before looking at the parent. This should resolve this kind of problem, assuming your have a similar setting.

不熟悉 iplanet,但在 WebSphere 中,您可以将应用程序类加载策略设置为 PARENT_LAST。这将在查看父级之前加载应用程序类加载器中的所有内容。假设您有类似的设置,这应该可以解决此类问题。

This does mean that you will have to supply all the dependencies in your own application (which is the best practice anyway).

这确实意味着您必须在自己的应用程序中提供所有依赖项(无论如何这是最佳实践)。