Apusic中log4j无法装载问题的解决

部署在Apusic 4.0.3上的应用中,使用到log4j,在应用装载时报如下异常:
2008-05-27 20:00:41 错误 [apusic.server.Main] 启动 'apusic:name=J2EEDeployer,j2eeType=Service' 服务失败。
java.lang.ExceptionInInitializerError
   at org.apache.struts.action.ActionServlet.initInternal(ActionServlet.java:1013)
   at org.apache.struts.action.ActionServlet.init(ActionServlet.java:322)
   at com.tjsoft.system.CTJActionServlet.init(CTJActionServlet.java:27)
   at javax.servlet.GenericServlet.init(GenericServlet.java:256)
   at com.apusic.servlet.http.ServletConfigImpl.initializeComponent(Unknown Source)
   at com.apusic.servlet.http.ServletConfigImpl.initializeComponent(Unknown Source)
   at com.apusic.servlet.http.ComponentConfigImpl.createComponent(Unknown Source)
   at com.apusic.servlet.http.ComponentConfigImpl.getComponent(Unknown Source)
   at com.apusic.servlet.http.ServletConfigImpl.getServlet(Unknown Source)
   at com.apusic.servlet.http.Context.loadStartupServlets(Unknown Source)
   at com.apusic.servlet.http.Context.start(Unknown Source)
   at com.apusic.servlet.http.VirtualHost.addContext(Unknown Source)
   at com.apusic.servlet.http.HttpServer.loadWebModule(Unknown Source)
   at com.apusic.servlet.http.WebService.loadWebModule(Unknown Source)
   at com.apusic.deploy.runtime.J2EEApplication.loadWebModule(Unknown Source)
   at com.apusic.deploy.runtime.J2EEApplication.startApplication(Unknown Source)
   at com.apusic.deploy.runtime.J2EEApplication.startService(Unknown Source)
   at com.apusic.service.Service.start(Unknown Source)
   at com.apusic.deploy.runtime.J2EEDeployer.startUserApplications(Unknown Source)
   at com.apusic.deploy.runtime.J2EEDeployer.startService(Unknown Source)
   at com.apusic.service.Service.start(Unknown Source)
   at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
   at java.lang.reflect.Method.invoke(Unknown Source)
   at com.apusic.jmx.MBeanDescriptor.invoke(Unknown Source)
   at com.apusic.jmx.DynamicMBeanSupport.invoke(Unknown Source)
   at com.sun.jmx.mbeanserver.DynamicMetaDataImpl.invoke(DynamicMetaDataImpl.java:221)
   at com.sun.jmx.mbeanserver.MetaDataImpl.invoke(MetaDataImpl.java:228)
   at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:822)
   at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:792)
   at com.apusic.server.J2EEServer.start(Unknown Source)
   at com.apusic.server.J2EEServer.startup(Unknown Source)
Caused by: org.apache.commons.logging.LogConfigurationException: org.apache.commons.logging.LogConfigurationException: No suitable Log constructor [Ljava.lang.Class;@20ca8b for org.apache.commons.logging.impl.Log4JCategoryLog
   at org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:532)
   at org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:272)
   at org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:246)
   at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:395)
   at org.apache.struts.util.MessageResources.(MessageResources.java:59)
   ... 32 more
Caused by: org.apache.commons.logging.LogConfigurationException: No suitable Log constructor [Ljava.lang.Class;@20ca8b for org.apache.commons.logging.impl.Log4JCategoryLog
   at org.apache.commons.logging.impl.LogFactoryImpl.getLogConstructor(LogFactoryImpl.java:432)
   at org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:525)
   ... 36 more
Caused by: java.lang.NoClassDefFoundError: org/apache/log4j/Category
   at java.lang.Class.getDeclaredConstructors0(Native Method)
   at java.lang.Class.privateGetDeclaredConstructors(Unknown Source)
   at java.lang.Class.getConstructor0(Unknown Source)
   at java.lang.Class.getConstructor(Unknown Source)
   at org.apache.commons.logging.impl.LogFactoryImpl.getLogConstructor(LogFactoryImpl.java:429)
   ... 37 more


引起此异常的原因是Apusic的system classloader中,已经加载了commons-logging类,而log4j的类是在下一级的servlet classloader中加载的,对system classloader加载的类来说不可见,因此报“java.lang.NoClassDefFoundError”。

解决的办法由多种,下面介绍常用的2种:

1. 将log4j的jar包加入到APUSIC_HOME/lib目录,并且在APUSIC_HOME/bin/startapusic.cmd(linux下是startapusic)中的java命令行中,通过添加 -Dlog4j.configuration=your_log4j_configuration_file指定log4j的配置文件。

2. 在APUSIC_HOME/bin/startapusic.cmd(linux下是startapusic)中的java命令行中,通过添加 -Dorg.apache.commons.logging.Log=org.apache.commons.logging.impl.Jdk14Logger

你可能感兴趣的:(java,apache,log4j,servlet,struts)