从控制台启动OSGI时Log4j2无法正常工作(在Eclipse中工作)

时间:2014-08-21 21:41:53

标签: java eclipse log4j osgi log4j2

我有一个使用log4j2在eclipse上运行的日志记录程序。我没有使用任何xml文件进行配置,而是以编程方式修改默认配置。我正在使用声明式服务在OSGi环境中开发。

当我从控制台运行OSGi并启动记录器包时,没有错误。但是,当我启动另一个使用此记录器捆绑服务的捆绑包时,会显示错误,显示:

Caused by: java.lang.ClassNotFoundException: javax.naming.NamingException 

我将整个错误日志放在这里,以防它提供详细信息。因此捆绑将无法解决。可能是造成这个问题的原因是什么?

!SESSION 2014-08-21 23:05:27.381 -----------------------------------------------
eclipse.buildId=unknown
java.version=1.7.0_51
java.vendor=Oracle Corporation
BootLoader constants: OS=win32, ARCH=x86_64, WS=win32, NL=en_US
Command-line arguments:  -console -consoleLog

!ENTRY comp4 4 0 2014-08-21 23:07:16.979
!MESSAGE [SCR] Error while trying to bind reference Reference[name = ILogger, interface = de.logger.ILogger, policy = static, cardinality = 1..1, target = null, bind = bindLoggerService, unbind = unbindLoggerService]
    Details:
    Problematic reference = Reference[name = ILogger, interface = de.logger.ILogger, policy = static, cardinality = 1..1, target = null, bind = bindLoggerService, unbind = unbindLoggerService]
    of service component = comp4
    component implementation class = de.comp4.finalTest
    located in bundle with symbolic name = comp4
    bundle location = file:\C:\Users\test2\plugins\comp4_1.0.0.jar
!STACK 0
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.eclipse.equinox.internal.ds.model.ComponentReference.bind(ComponentReference.java:367)
    at org.eclipse.equinox.internal.ds.model.ServiceComponentProp.bindReference(ServiceComponentProp.java:430)
    at org.eclipse.equinox.internal.ds.model.ServiceComponentProp.bind(ServiceComponentProp.java:218)
    at org.eclipse.equinox.internal.ds.model.ServiceComponentProp.build(ServiceComponentProp.java:344)
    at org.eclipse.equinox.internal.ds.InstanceProcess.buildComponent(InstanceProcess.java:588)
    at org.eclipse.equinox.internal.ds.InstanceProcess.buildComponents(InstanceProcess.java:196)
    at org.eclipse.equinox.internal.ds.Resolver.buildNewlySatisfied(Resolver.java:441)
    at org.eclipse.equinox.internal.ds.Resolver.enableComponents(Resolver.java:213)
    at org.eclipse.equinox.internal.ds.SCRManager.performWork(SCRManager.java:800)
    at org.eclipse.equinox.internal.ds.SCRManager$QueuedJob.dispatch(SCRManager.java:767)
    at org.eclipse.equinox.internal.ds.WorkThread.run(WorkThread.java:89)
    at org.eclipse.equinox.internal.util.impl.tpt.threadpool.Executor.run(Executor.java:70)
Caused by: java.lang.NoClassDefFoundError: javax/naming/NamingException
    at org.apache.logging.log4j.core.lookup.Interpolator.<init>(Interpolator.java:75)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.<init>(AbstractConfiguration.java:99)
    at org.apache.logging.log4j.core.config.NullConfiguration.<init>(NullConfiguration.java:29)
    at org.apache.logging.log4j.core.LoggerContext.<clinit>(LoggerContext.java:63)
    at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.locateContext(ClassLoaderContextSelector.java:218)
    at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.getContext(ClassLoaderContextSelector.java:125)
    at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.getContext(ClassLoaderContextSelector.java:80)
    at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:72)
    at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:37)
    at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:473)
    at de.logger.tinylogger.TinyLogger.setClassName(TinyLogger.java:1097)
    at de.comp4.finalTest.bindLoggerService(finalTest.java:27)
    ... 16 more
Caused by: java.lang.ClassNotFoundException: javax.naming.NamingException
    at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:513)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:429)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:417)
    at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:107)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 28 more
Root exception:
java.lang.NoClassDefFoundError: javax/naming/NamingException
    at org.apache.logging.log4j.core.lookup.Interpolator.<init>(Interpolator.java:75)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.<init>(AbstractConfiguration.java:99)
    at org.apache.logging.log4j.core.config.NullConfiguration.<init>(NullConfiguration.java:29)
    at org.apache.logging.log4j.core.LoggerContext.<clinit>(LoggerContext.java:63)
    at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.locateContext(ClassLoaderContextSelector.java:218)
    at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.getContext(ClassLoaderContextSelector.java:125)
    at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.getContext(ClassLoaderContextSelector.java:80)
    at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:72)
    at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:37)
    at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:473)
    at de.logger.tinylogger.TinyLogger.setClassName(TinyLogger.java:1097)
    at de.comp4.finalTest.bindLoggerService(finalTest.java:27)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.eclipse.equinox.internal.ds.model.ComponentReference.bind(ComponentReference.java:367)
    at org.eclipse.equinox.internal.ds.model.ServiceComponentProp.bindReference(ServiceComponentProp.java:430)
    at org.eclipse.equinox.internal.ds.model.ServiceComponentProp.bind(ServiceComponentProp.java:218)
    at org.eclipse.equinox.internal.ds.model.ServiceComponentProp.build(ServiceComponentProp.java:344)
    at org.eclipse.equinox.internal.ds.InstanceProcess.buildComponent(InstanceProcess.java:588)
    at org.eclipse.equinox.internal.ds.InstanceProcess.buildComponents(InstanceProcess.java:196)
    at org.eclipse.equinox.internal.ds.Resolver.buildNewlySatisfied(Resolver.java:441)
    at org.eclipse.equinox.internal.ds.Resolver.enableComponents(Resolver.java:213)
    at org.eclipse.equinox.internal.ds.SCRManager.performWork(SCRManager.java:800)
    at org.eclipse.equinox.internal.ds.SCRManager$QueuedJob.dispatch(SCRManager.java:767)
    at org.eclipse.equinox.internal.ds.WorkThread.run(WorkThread.java:89)
    at org.eclipse.equinox.internal.util.impl.tpt.threadpool.Executor.run(Executor.java:70)
Caused by: java.lang.ClassNotFoundException: javax.naming.NamingException
    at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:513)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:429)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:417)
    at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:107)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 28 more

!ENTRY comp4 4 0 2014-08-21 23:07:17.006
!MESSAGE [SCR] Error while trying to bind reference Reference[name = ILogger, interface = de.logger.ILogger, policy = static, cardinality = 1..1, target = null, bind = bindLoggerService, unbind = unbindLoggerService]
    Details:
    Problematic reference = Reference[name = ILogger, interface = de.logger.ILogger, policy = static, cardinality = 1..1, target = null, bind = bindLoggerService, unbind = unbindLoggerService]
    of service component = comp4
    component implementation class = de.comp4.finalTest
    located in bundle with symbolic name = comp4
    bundle location = file:\C:\test2\plugins\comp4_1.0.0.jar
!STACK 0
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.eclipse.equinox.internal.ds.model.ComponentReference.bind(ComponentReference.java:367)
    at org.eclipse.equinox.internal.ds.model.ServiceComponentProp.bindReference(ServiceComponentProp.java:430)
    at org.eclipse.equinox.internal.ds.model.ServiceComponentProp.bind(ServiceComponentProp.java:218)
    at org.eclipse.equinox.internal.ds.model.ServiceComponentProp.build(ServiceComponentProp.java:344)
    at org.eclipse.equinox.internal.ds.InstanceProcess.buildComponent(InstanceProcess.java:588)
    at org.eclipse.equinox.internal.ds.InstanceProcess.buildComponents(InstanceProcess.java:196)
    at org.eclipse.equinox.internal.ds.Resolver.buildNewlySatisfied(Resolver.java:441)
    at org.eclipse.equinox.internal.ds.Resolver.enableComponents(Resolver.java:213)
    at org.eclipse.equinox.internal.ds.SCRManager.performWork(SCRManager.java:800)
    at org.eclipse.equinox.internal.ds.SCRManager$QueuedJob.dispatch(SCRManager.java:767)
    at org.eclipse.equinox.internal.ds.WorkThread.run(WorkThread.java:89)
    at org.eclipse.equinox.internal.util.impl.tpt.threadpool.Executor.run(Executor.java:70)
Caused by: java.lang.NoClassDefFoundError: javax/naming/NamingException
    at org.apache.logging.log4j.core.lookup.Interpolator.<init>(Interpolator.java:75)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.<init>(AbstractConfiguration.java:99)
    at org.apache.logging.log4j.core.config.NullConfiguration.<init>(NullConfiguration.java:29)
    at org.apache.logging.log4j.core.LoggerContext.<clinit>(LoggerContext.java:63)
    at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.locateContext(ClassLoaderContextSelector.java:218)
    at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.getContext(ClassLoaderContextSelector.java:125)
    at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.getContext(ClassLoaderContextSelector.java:80)
    at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:72)
    at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:37)
    at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:473)
    at de.logger.tinylogger.TinyLogger.setClassName(TinyLogger.java:1097)
    at de.comp4.finalTest.bindLoggerService(finalTest.java:27)
    ... 16 more
Caused by: java.lang.ClassNotFoundException: javax.naming.NamingException
    at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:513)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:429)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:417)
    at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:107)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 28 more
Root exception:
java.lang.NoClassDefFoundError: javax/naming/NamingException
    at org.apache.logging.log4j.core.lookup.Interpolator.<init>(Interpolator.java:75)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.<init>(AbstractConfiguration.java:99)
    at org.apache.logging.log4j.core.config.NullConfiguration.<init>(NullConfiguration.java:29)
    at org.apache.logging.log4j.core.LoggerContext.<clinit>(LoggerContext.java:63)
    at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.locateContext(ClassLoaderContextSelector.java:218)
    at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.getContext(ClassLoaderContextSelector.java:125)
    at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.getContext(ClassLoaderContextSelector.java:80)
    at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:72)
    at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:37)
    at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:473)
    at de.logger.tinylogger.TinyLogger.setClassName(TinyLogger.java:1097)
    at de.comp4.finalTest.bindLoggerService(finalTest.java:27)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.eclipse.equinox.internal.ds.model.ComponentReference.bind(ComponentReference.java:367)
    at org.eclipse.equinox.internal.ds.model.ServiceComponentProp.bindReference(ServiceComponentProp.java:430)
    at org.eclipse.equinox.internal.ds.model.ServiceComponentProp.bind(ServiceComponentProp.java:218)
    at org.eclipse.equinox.internal.ds.model.ServiceComponentProp.build(ServiceComponentProp.java:344)
    at org.eclipse.equinox.internal.ds.InstanceProcess.buildComponent(InstanceProcess.java:588)
    at org.eclipse.equinox.internal.ds.InstanceProcess.buildComponents(InstanceProcess.java:196)
    at org.eclipse.equinox.internal.ds.Resolver.buildNewlySatisfied(Resolver.java:441)
    at org.eclipse.equinox.internal.ds.Resolver.enableComponents(Resolver.java:213)
    at org.eclipse.equinox.internal.ds.SCRManager.performWork(SCRManager.java:800)
    at org.eclipse.equinox.internal.ds.SCRManager$QueuedJob.dispatch(SCRManager.java:767)
    at org.eclipse.equinox.internal.ds.WorkThread.run(WorkThread.java:89)
    at org.eclipse.equinox.internal.util.impl.tpt.threadpool.Executor.run(Executor.java:70)
Caused by: java.lang.ClassNotFoundException: javax.naming.NamingException
    at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:513)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:429)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:417)
    at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:107)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 28 more

!ENTRY org.eclipse.equinox.ds 4 0 2014-08-21 23:07:17.037
!MESSAGE The component was not built because some of its references could not be bound. The component is Component[
    name = comp4
    activate = activate
    deactivate = deactivate
    modified = 
    configuration-policy = optional
    factory = null
    autoenable = true
    immediate = true
    implementation = de.comp4.finalTest
    state = Unsatisfied
    properties = 
    serviceFactory = false
    serviceInterface = null
    references = {
        Reference[name = ILogger, interface = de.logger.ILogger, policy = static, cardinality = 1..1, target = null, bind = bindLoggerService, unbind = unbindLoggerService]
    }
    located in bundle = comp4_1.0.0 [18]
]

!ENTRY org.eclipse.equinox.ds 4 0 2014-08-21 23:07:17.041
!MESSAGE The component was not built because some of its references could not be bound. The component is Component[
    name = comp4
    activate = activate
    deactivate = deactivate
    modified = 
    configuration-policy = optional
    factory = null
    autoenable = true
    immediate = true
    implementation = de.comp4.finalTest
    state = Unsatisfied
    properties = 
    serviceFactory = false
    serviceInterface = null
    references = {
        Reference[name = ILogger, interface = de.logger.ILogger, policy = static, cardinality = 1..1, target = null, bind = bindLoggerService, unbind = unbindLoggerService]
    }
    located in bundle = comp4_1.0.0 [18]
]

编辑:

我在一个简单的设置中尝试使用LogManager.getLogger()而没有配置代码(左边是默认值),但结果仍然相同。如果我没有调用包含“LogManager.getLogger()”的方法,则包含Log4j2的包只会解析。

....Inside logger...
private static Logger m_Logger;
public void setClassName(String className) 
    {
    //Logger m_Logger = LogManager.getLogger(className); 
    Logger m_Logger = LogManager.getLogger(); 
    m_Logger.error("Logger program working");
    }
....Inside consuming bundle...
protected void OSGiLoggerBind (OSGiLogger myLogger)
    {
    m_LoggerService = myLogger;
m_LoggerService.setClassName(OSGiLoggerTest.class.getName());
    }

1 个答案:

答案 0 :(得分:0)

这实际上是一个错误。 LOG4J2在OSGi环境中具有类here中的类加载器问题,同样here也存在类加载器问题。

我之前使用的是下载版本apache-log4j-2.0-rc2-bin。 但现在有一个新版本here apache-log4j-2.0.2-bin,它在OSGi环境下运行。但是对于正确解析LOG4j2的包,我必须导入“org.osgi.framework.wiring”。

它仍然显示错误“无法重新配置JMX java.lang.NoClassDefFoundError:javax / management / ObjectName ....”我仍然需要找到它的解决方案,但至少它在OSGi环境中运行从控制台开始。