Skip to content

JNR doesn't respect context classloader #8

Description

@btiernay

See jnr/jnr-ffi#51 for the issue.

This could be fixed in the library using something like:

  /**
   * Workaround for unfortunate system class loader usage.
   * 
   * @see https://github.com/jnr/jnr-ffi/issues/51
   */
  @SneakyThrows
  private void patchFfi() {
    ClosureManager closureManager = NativeRuntime.getInstance().getClosureManager();

    // Get inner class loader
    val classLoader = findField(closureManager.getClass(), "classLoader");
    classLoader.setAccessible(true);
    val asmClassLoader = classLoader.get(closureManager);

    // Update to use context class loader over the default system class loader
    val parent = findField(asmClassLoader.getClass(), "parent");
    parent.setAccessible(true);
    parent.set(asmClassLoader, Thread.currentThread().getContextClassLoader());
  }

Thoughts?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions