Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support context class loader in NativeRuntime's closureManager #51

Closed
btiernay opened this issue Oct 3, 2015 · 10 comments
Closed

Support context class loader in NativeRuntime's closureManager #51

btiernay opened this issue Oct 3, 2015 · 10 comments
Milestone

Comments

@btiernay
Copy link

btiernay commented Oct 3, 2015

In some circumstances it would be useful to replace:

  private final NativeClosureManager closureManager = new NativeClosureManager(this,
            new SignatureTypeMapperAdapter(new DefaultTypeMapper()),
            new AsmClassLoader(ClassLoader.getSystemClassLoader()));

with

  private final NativeClosureManager closureManager = new NativeClosureManager(this,
            new SignatureTypeMapperAdapter(new DefaultTypeMapper()),
            new AsmClassLoader(Thread.currentThread().getContextClassLoader()));

A good example is with Spring Boot's nested jars which have the following restriction:

Launched applications should use Thread.getContextClassLoader() when loading classes (most libraries and frameworks will do this by default). Trying to load nested jar classes via ClassLoader.getSystemClassLoader() will fail.

Please see http://www.javaworld.com/article/2077344/core-java/find-a-way-out-of-the-classloader-maze.html for a good discussion of this.

@btiernay
Copy link
Author

btiernay commented Oct 3, 2015

I was able to work around this issue by doing the following:

    ClosureManager closureManager = NativeRuntime.getInstance().getClosureManager();
    val classLoader = findField(closureManager.getClass(), "classLoader");
    classLoader.setAccessible(true);
    val asmClassLoader = classLoader.get(closureManager);

    val parent = findField(asmClassLoader.getClass(), "parent");
    parent.setAccessible(true);
    parent.set(asmClassLoader, Thread.currentThread().getContextClassLoader());

@mkristian
Copy link
Contributor

this means when we switch to the context classloader that in OSGi case it can not find its classes since OSGi does not work with context classloader at all.

but probably you are right in the respect that ClassLoader.getSystemClassLoader() is not the right thing. I would say that getClass().getClassLoader() is the better choice which will work when everything is loaded from the context classloader and with the OSGi case as well.

@btiernay
Copy link
Author

That would be a better default. Also, would it be possible to make this configurable maybe?

@headius
Copy link
Member

headius commented Sep 26, 2016

See contributed test case in #55.

@neupsh
Copy link

neupsh commented Dec 31, 2016

Is there any update on this issue? I was trying to use jnr related java library in clojure and when i run the project with boot-clj build tool, it fails to load the class with:

java.lang.ClassNotFoundException: jnr.ffi.provider.jffi.NativeClosureProxy
             java.lang.NoClassDefFoundError: jnr/ffi/provider/jffi/NativeClosureProxy
                 java.lang.RuntimeException: java.lang.NoClassDefFoundError: jnr/ffi/provider/jffi/NativeClosureProxy

This is because, boot-clj runs 'boot-tasks' in 'pods', which are clojure runtimes in single JVM. They are separated by different classloaders and thus the dependencies are tied to these different classloaders. The System ClassLoader does not have any libraries in the classpath and as a result, it fails to load the class.

@mkristian
Copy link
Contributor

will have look into this first thing next year :)

@headius
Copy link
Member

headius commented Jan 25, 2017

Let's try fixing this with getClass().getClassLoader() falling back on ClassLoader.getSystemClassLoader() when that is null. I'll commit that and y'all can test it.

@headius headius added this to the 2.1.3 milestone Jan 25, 2017
@headius
Copy link
Member

headius commented Jan 25, 2017

I've pushed a fix and will release 2.1.3 shortly.

@gzunino
Copy link

gzunino commented Jan 27, 2017

This is not enough for OSGi running jnr as bundle, when a closure defines a return type with a Class defined in another bundle. For example:

BundleA > BundleJNR (BundleA depends on BundleJNR)

a callback on BundleA with a return type ClassA, being ClassA a class on BundleA.

JNR tries to load ClassA with the classloader of BundleJNR and fails to do it.

@realPyR3X
Copy link

Has this issue been resolved for OSGi? I'm trying to use JavaFS which relies on this library and I continue to get The activate method has thrown an exception

The activate method has thrown an exception java.lang.RuntimeException: java.lang.NoClassDefFoundError: jnr/ffi/provider/jffi/AsmStructByReferenceFromNativeConverter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants