[ 
https://issues.apache.org/jira/browse/LOG4J2-1753?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15781531#comment-15781531
 ] 

Ludovic HOCHET commented on LOG4J2-1753:
----------------------------------------

The optional import of core.osgi is the result of LOG4J2-920, which was similar 
to the current issue: a class defined in Core is being instantiated from API, 
in order for that to happen the package where it is defined need first to be 
exported from Core then imported in API.

More precisely, the code calls LoaderUtil::loadClass in API, and within that 
method it tries to load the class from the ThreadContextClassLoader and fails 
(for the tests nor the LOG4J2-920 test app it is the application class loader, 
ie the class path which does not have the Log4J classes), it then tries 
Class.forName(className) which will use the class loader for LoaderUtil that is 
it will use the class loader for the API bundle and therefore will need to 
import the package containing the class.

An alternative could be to use Class.forName(className, true, 
classLoaderFromTheBundleThatContainClassName) but that would require to 
identify the bundle from which to load className.

Other alternatives may be to look at Java services (java.util.ServiceLoader) 
and OSGi services.


> java.lang.ClassNotFoundException: 
> org.apache.logging.log4j.core.util.ExecutorServices when running the OSGi 
> tests
> -----------------------------------------------------------------------------------------------------------------
>
>                 Key: LOG4J2-1753
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-1753
>             Project: Log4j 2
>          Issue Type: Bug
>            Reporter: Ludovic HOCHET
>         Attachments: patch-log4j2-1753.diff
>
>
> When running the OSGi tests for LOG4J2-1664, a new 
> java.lang.ClassNotFoundException: 
> org.apache.logging.log4j.core.util.ExecutorServices (for both Felix and 
> Equinox) appeared recently.
> Here is the Felix stack trace:
> {noformat}
> ERROR StatusLogger Failed to preload ExecutorServices class.
>  java.lang.ClassNotFoundException: *** Class 
> 'org.apache.logging.log4j.core.util.ExecutorServices' was not found because 
> bundle org.apache.logging.log4j.api [1] does not import 
> 'org.apache.logging.log4j.core.util' even though bundle 
> org.apache.logging.log4j.core [2] does export it. To resolve this issue, add 
> an import for 'org.apache.logging.log4j.core.util' to bundle 
> org.apache.logging.log4j.api [1]. ***
>       at 
> org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:2011)
>       at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
>       at java.lang.Class.forName0(Native Method)
>       at java.lang.Class.forName(Class.java:264)
>       at 
> org.apache.logging.log4j.util.LoaderUtil.loadClass(LoaderUtil.java:141)
>       at 
> org.apache.logging.log4j.core.LoggerContext.<clinit>(LoggerContext.java:74)
>       at 
> org.apache.logging.log4j.core.osgi.BundleContextSelector.locateContext(BundleContextSelector.java:67)
>       at 
> org.apache.logging.log4j.core.osgi.BundleContextSelector.getContext(BundleContextSelector.java:53)
>       at 
> org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.getContext(ClassLoaderContextSelector.java:57)
>       at 
> org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:147)
>       at 
> org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:45)
>       at org.apache.logging.log4j.LogManager.getContext(LogManager.java:194)
>       at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:551)
>       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>       at 
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
>       at 
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>       at java.lang.reflect.Method.invoke(Method.java:498)
>       at 
> org.apache.logging.log4j.osgi.tests.AbstractLoadBundleTest.log(AbstractLoadBundleTest.java:100)
>       at 
> org.apache.logging.log4j.osgi.tests.AbstractLoadBundleTest.testSimpleLogInAnOsgiContext(AbstractLoadBundleTest.java:279)
>       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>       at 
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
>       at 
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>       at java.lang.reflect.Method.invoke(Method.java:498)
>       at 
> org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
>       at 
> org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
>       at 
> org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
>       at 
> org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
>       at 
> org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
>       at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:48)
>       at org.junit.rules.RunRules.evaluate(RunRules.java:20)
>       at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
>       at 
> org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
>       at 
> org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
>       at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
>       at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
>       at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
>       at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
>       at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
>       at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
>       at 
> org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:367)
>       at 
> org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:274)
>       at 
> org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:238)
>       at 
> org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:161)
>       at 
> org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:290)
>       at 
> org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:242)
>       at 
> org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:121)
> Caused by: java.lang.ClassNotFoundException: 
> org.apache.logging.log4j.core.util.ExecutorServices not found by 
> org.apache.logging.log4j.api [1]
>       at 
> org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1558)
>       at 
> org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringImpl.java:79)
>       at 
> org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:1998)
>       ... 45 more
> {noformat}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscr...@logging.apache.org
For additional commands, e-mail: log4j-dev-h...@logging.apache.org

Reply via email to