问题:
折腾半天环境,终于能跑起来,可以我加了两个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参数里面,删除安全选项参数。好了,世界又恢复正常了。