Title: Returning contextClassLoader in Platfom.getClassLoader() can mess up in an OSGI application · Issue #574 · nativelibs4java/nativelibs4java · GitHub
Open Graph Title: Returning contextClassLoader in Platfom.getClassLoader() can mess up in an OSGI application · Issue #574 · nativelibs4java/nativelibs4java
X Title: Returning contextClassLoader in Platfom.getClassLoader() can mess up in an OSGI application · Issue #574 · nativelibs4java/nativelibs4java
Description: TL;DR: It should be possible to force Platfom.getClassLoader(Class>) to prefer the classloader of the class over the context classloader of the thread. Details: I am running JavaCL in an OSGi bundle (a plugin for a GUI application call...
Open Graph Description: TL;DR: It should be possible to force Platfom.getClassLoader(Class>) to prefer the classloader of the class over the context classloader of the thread. Details: I am running JavaCL in an OSGi bun...
X Description: TL;DR: It should be possible to force Platfom.getClassLoader(Class<?>) to prefer the classloader of the class over the context classloader of the thread. Details: I am running JavaCL in an OS...
Opengraph URL: https://github.com/nativelibs4java/nativelibs4java/issues/574
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Returning contextClassLoader in Platfom.getClassLoader() can mess up in an OSGI application","articleBody":"**TL;DR:** It should be possible to force Platfom.getClassLoader(Class\u003c?\u003e) to prefer the classloader of the class over the context classloader of the thread.\n\n---\n\n**Details:** I am running JavaCL in an OSGi bundle (a plugin for a GUI application called Cytoscape). The plugin provides methods that are executed on threads created by the application framework. I try to call kernels from the application-created thread, which results in some class loading for event callbacks with CallbackNativeImplementer. But that throws NoClassDefFoundError (stacktrace below). \n\nAfter some funny debugging, I realized that the problem is that the parent classloader of CallbackNativeImplementer is NOT the bundle classloader - it is the classloader returned by Platfom.getClassLoader() (set in the constructor of CallbackNativeImplementer). And Platfom.getClassLoader() first checks Thread.currentThread().getContextClassLoader() which returns the class loader of the application framework, which does not see the bundle resources.\n\nAs a workaround, I use Thread.currentThread().setContextClassLoader() to set the correct class loader before invoking any JavaCL methods, but that seems hacky. I am not sure for what use cases the context classloader is more appropriate than the classloader of the active class, but maybe there could be some setting to give preference to the class's classloader over the thread... \n\nOr is there some other way to enforce proper classloader in this case?\n\nI'll be happy to work on this issue once someone from the team confirms what is the correct way to address it.\n\nNote that I am not using the OSGi version of JavaCL, instead, I embed the JavaCL jars within my bundle (with \u003cEmbed-Dependency\u003e directive of Maven bundle plugin) as this is mandated by the Cytoscape plugin development rules. \nI however think that the problem would arise the same if JavaCL was used as an OSGi bundle (the calling thread would still provide different class loader). \nI did some tests with having JavaCL as a separete bundle and the problem did not go away, although I am not certain I did everything well.\n\nThe promised stacktrace:\n\n```\nCaused by: java.lang.NoClassDefFoundError: com/nativelibs4java/opencl/library/IOpenCLLibrary$clSetEventCallback_arg1_callback\n at java.lang.ClassLoader.defineClass1(Native Method)\n at java.lang.ClassLoader.defineClass(ClassLoader.java:763)\n at java.lang.ClassLoader.defineClass(ClassLoader.java:642)\n at org.bridj.CallbackNativeImplementer.defineClass(CallbackNativeImplementer.java:214)\n at org.bridj.CallbackNativeImplementer.getCallbackImplType(CallbackNativeImplementer.java:103)\n at org.bridj.CRuntime.register(CRuntime.java:426)\n at org.bridj.CRuntime.registerFamily(CRuntime.java:483)\n at org.bridj.CRuntime.register(CRuntime.java:470)\n at org.bridj.CRuntime.register(CRuntime.java:345)\n at org.bridj.CRuntime$CTypeInfo.\u003cinit\u003e(CRuntime.java:126)\n at org.bridj.CRuntime.getTypeInfo(CRuntime.java:336)\n at org.bridj.BridJ.getTypeInfo(BridJ.java:377)\n at org.bridj.BridJ.initialize(BridJ.java:1126)\n at org.bridj.NativeObject.\u003cinit\u003e(NativeObject.java:50)\n at org.bridj.Callback.\u003cinit\u003e(Callback.java:60)\n at com.nativelibs4java.opencl.library.IOpenCLLibrary$clSetEventCallback_arg1_callback.\u003cinit\u003e(IOpenCLLibrary.java:576)\n at com.nativelibs4java.opencl.CLEvent$3.\u003cinit\u003e(CLEvent.java:128)\n at com.nativelibs4java.opencl.CLEvent.\u003cclinit\u003e(CLEvent.java:128)\n at com.nativelibs4java.opencl.CLKernel.enqueueNDRange(CLKernel.java:482)\n at cz.cas.mbu.gn.compute.GNCompute.executeKernel(GNCompute.java:121)\n at cz.cas.mbu.gn.compute.GNCompute.computeAdditiveRegulation(GNCompute.java:342)\n at cz.cas.mbu.cygngpu.internal.PredictSingleRegulationsTask.run(PredictSingleRegulationsTask.java:176)\n at org.cytoscape.work.internal.task.JDialogTaskManager$TaskRunnable.innerRun(JDialogTaskManager.java:326)\n at org.cytoscape.work.internal.task.JDialogTaskManager$TaskRunnable.run(JDialogTaskManager.java:355)\n ... 5 more\nCaused by: java.lang.ClassNotFoundException: com.nativelibs4java.opencl.library.IOpenCLLibrary$clSetEventCallback_arg1_callback\n at java.lang.ClassLoader.findClass(ClassLoader.java:530)\n at java.lang.ClassLoader.loadClass(ClassLoader.java:424)\n at java.lang.ClassLoader.loadClass(ClassLoader.java:357)\n ... 29 more\n```\n","author":{"url":"https://github.com/martinmodrak","@type":"Person","name":"martinmodrak"},"datePublished":"2016-06-28T10:57:31.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/574/nativelibs4java/issues/574"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:d4c44378-22c8-a78b-5feb-736fa70fa4d3 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9B34:3D4A65:75650A:9FFAC4:697BD8B2 |
| html-safe-nonce | 33d90a5e31d9cf3c8ff13eee197f72bce2a8ceed9130cbf8a5c0fdfb7d717987 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QjM0OjNENEE2NTo3NTY1MEE6OUZGQUM0OjY5N0JEOEIyIiwidmlzaXRvcl9pZCI6Ijc2NDIzMTM5NjQ3MjU3MjEyNjYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 67f150a50bd0991ddcb6d2ed7251fd61de35664350127c9cfc2ba695930f1883 |
| hovercard-subject-tag | issue:162658151 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/nativelibs4java/nativelibs4java/574/issue_layout |
| twitter:image | https://opengraph.githubassets.com/11492bc74c712415750acfb9f030a307eecf229ca886e0cd63b2570ee27f23f7/nativelibs4java/nativelibs4java/issues/574 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/11492bc74c712415750acfb9f030a307eecf229ca886e0cd63b2570ee27f23f7/nativelibs4java/nativelibs4java/issues/574 |
| og:image:alt | TL;DR: It should be possible to force Platfom.getClassLoader(Class>) to prefer the classloader of the class over the context classloader of the thread. Details: I am running JavaCL in an OSGi bun... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | martinmodrak |
| hostname | github.com |
| expected-hostname | github.com |
| None | d9273cb6ee4e18acd2ce5be1d6a9f9b3984f728c67ac95709fc3d38fedbb2967 |
| turbo-cache-control | no-preview |
| go-import | github.com/nativelibs4java/nativelibs4java git https://github.com/nativelibs4java/nativelibs4java.git |
| octolytics-dimension-user_id | 11545921 |
| octolytics-dimension-user_login | nativelibs4java |
| octolytics-dimension-repository_id | 2194042 |
| octolytics-dimension-repository_nwo | nativelibs4java/nativelibs4java |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 2194042 |
| octolytics-dimension-repository_network_root_nwo | nativelibs4java/nativelibs4java |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 784b44795f41068c6521c17f38ad6ed44ce1775c |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width