maven jar包冲突: cannot be cast to ch.qos.logback.classic.LoggerContext

问题:

     折腾半天环境,终于能跑起来,可以我加了两个maven包之后,又起不来不了。报错如下:

ClassCastException: org.slf4j.impl.Log4jLoggerFactory cannot be cast to ch.qos.logback.classic.LoggerContext


看日志,提示是:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar。。。。。

SLF4J: Found binding in [jar。。。。。

表明已经有多个logging实现,如logback/slf4j-log4j/slf4j-jcl...

确保在同一个classpath下只有一个logging实现。

这个工程日志是使用logback的。

解决:

找到提示冲突的jar,就是logback跟新加入的jar。保留logback。所以修改冲突jar,加入排除规则

<exclusions>
				<exclusion>
					<artifactId>log4j-slf4j-impl</artifactId>
					<groupId>org.apache.logging.log4j</groupId>
				</exclusion>
			</exclusions>
这里,在说一下。可以直接修改pom.xml.也是在eclipse图形界面:

重新打包,OK

还记起一个问题,与这个有关系的错误,以前还遇到过:

java.lang.NoClassDefFoundError: Could not initialize class org.apache.log4j.Log4

看着像jar包缺失,其实从pom里面可以找到对应jar,那么就怀疑是jar冲突了。

log4j-over-slf4j.jar 和 slf4j-log4j12.jar 解决还是从相关jar排除掉

<exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>

********************************我是分割线*****************************************

在顺便记一下,自己无意识的的一个动作,导致的报错。在eclipse的server界面上,如图:

不小心勾选了启动security,开始我还没注意,直到启动报错如下才发现:

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
java.lang.ExceptionInInitializerError
	at org.apache.logging.log4j.status.StatusLogger.<clinit>(StatusLogger.java:55)
	at org.apache.logging.log4j.util.ReflectionUtil.<clinit>(ReflectionUtil.java:52)
	at org.apache.logging.log4j.jcl.LogAdapter.getContext(LogAdapter.java:39)
	at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getLogger(AbstractLoggerAdapter.java:42)
	at org.apache.logging.log4j.jcl.LogFactoryImpl.getInstance(LogFactoryImpl.java:40)
	at org.apache.logging.log4j.jcl.LogFactoryImpl.getInstance(LogFactoryImpl.java:55)
	at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:657)
	at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:282)
	at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5099)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5615)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1571)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1561)
	at java.util.concurrent.FutureTask.run(FutureTask.java:262)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
	at java.lang.Thread.run(Thread.java:745)
Caused by: java.security.AccessControlException: access denied ("java.lang.RuntimePermission" "getClassLoader")
	at java.security.AccessControlContext.checkPermission(AccessControlContext.java:372)
	at java.security.AccessController.checkPermission(AccessController.java:559)
	at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
	at java.lang.ClassLoader.checkClassLoaderPermission(ClassLoader.java:1561)
	at java.lang.ClassLoader.getSystemClassLoader(ClassLoader.java:1480)
	at org.apache.logging.log4j.util.LoaderUtil.findUrlResources(LoaderUtil.java:192)
	at org.apache.logging.log4j.util.LoaderUtil.findResources(LoaderUtil.java:183)
	at org.apache.logging.log4j.util.PropertiesUtil.<init>(PropertiesUtil.java:90)
	at org.apache.logging.log4j.util.PropertiesUtil.<clinit>(PropertiesUtil.java:36)
	... 18 more
网上搜了下,有人给出的解决办法是,修改工程所对应的jre的policy。

这是我本机的路径:C:\Java\jdk1.7.0_79\jre\lib\security

修改Java.policy,在grant里面加入如“permission java.lang.RuntimePermission "getClassLoader";”

总之就是那里报错缺少什么权限就加什么,缺一两个还好。要是多了的话,我就劝你放弃了,看看是否有必要加,因为更直接的办法就是去掉那个安全选项,或者在server的vm参数里面,删除安全选项参数。好了,世界又恢复正常了。



  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值