Skip to content

Additions to j4rs rust-side Jvm to support custom thread classloader configuration #150

Closed

Description

@toastertaster

In continuation to #149 , I am opening this discussion on potential additional changes to support a custom thread ContextClassLoader. The Java17 feature removal of a custom SystemClassLoader has another ramification that I want to fix in a separate PR once its ready. When new threads are formed, they inherit a Thread ContextClassLoader from their parent thread, which in many cases might be the SystemClassLoader. When I could substitute the scl, I was able to ensure the new threads had a ClassLoader that could reach their necessary classes because eventually every ClassLoader had a parent ClassLoader of the scl. Since this is not possible in newer versions of Java, I have created a temporary workaround as I explore options to fix the problem. I think in time I could make a better long-term feature addition. Some example code of how the problem manifests on the rust-side:

let jvm: Jvm = Jvm::attach_thread().expect("unable to attach to the JVM");
// this jvm might not have the necessary ClassLoader.  
// It will always have j4rs and its dependencies, but not necessarily additional Java classes that the subsequent rust code relies on.

// my current quick-fix approach that does jvm.invoke_static("java.lang.Thread"...)... code to update the ClassLoader.
update_thread_classloader(&jvm); 
// now calls to jvm.invoke() and jvm.invoke_static() can work with custom Classes outside of original j4rs+dependencies
// Unfortunately to do this quickly, I am holding a reference to a j4rs Instance of the ClassLoader of interest 
// and provide it as an InvocationArg to the invoke() call.

// Instead of my custom workaround above, two potential designs I am exploring:

// a) add a second jvm method call that sets up the classloader.  Something like
jvm.set_thread_classloader(...parameters...)

// b) Do everything in a single Jvm method call, replacing the attach_thread() line with
let jvm = Jvm::attach_thread_with_classloader(...parameters...)

The design work is to figure out how we want to hold onto the Instance<ClassLoader>, such as add a Field to the Java-side j4rs code or other mechanism that makes it available to j4rs rust-side code. @astonbitecode, if you have any preferences to the implementation of this feature, I will see about getting this change done. Thank you.

Activity

  1. astonbitecode commented on Sep 16, 2025

    @astonbitecode
    Owner

    I am trying to find some time understanding and dealing with this so, please @toastertaster, let's get this discussion going.

    Generally, j4rs uses a custom classloader, the org.astonbitecode.j4rs.api.deploy.J4rsClassLoader, which is set during JVM initialization via the jvm option java.system.class.loader. This is why we may add classes to the classpath at any time, even after the initialization (using the Jvm.deploy_artifact).

    In case you want a different classloader you could for example initialize the Jvm like:

    let jvm: Jvm = JvmBuilder::new()
        .with_default_classloader()
        .java_opt(JavaOpt::new("-Djava.system.class.loader=my.OtherClassloader"))
    .build()?;

    This way, the my.OtherClassloader will be the System classloader and will be used by j4rs.

    The mention of eclipse plugins in your PR and the special classloaders / class handling, gives me the impression that you try to use j4rs in an OSGi environment. Is this true?

  2. toastertaster commented on Sep 18, 2025

    @toastertaster
    Author

    I have been using j4rs inside of an Eclipse IDE plugin. Eclipse-IDE-based programs set up a sophisticated Java program design with a main program app ClassLoader, and then plugins can configure to have their own ClassLoader hierarchy if they want to have a privacy layer on their provided classes. Because my code is in a plugin as part of a larger program (~30 plugins), I have avoided changing any of the original application configuration (ie, we have not modified any of the JavaVM loading configuration command-line flags, etc), or modifying external plugins loading configuration. The j4rs-with-dependencies.jar is made available on the ClassLoader for my plugin. Using j4rs this way has worked very well without needing to modify the original application configuration files, and we've been using j4rs for several years in this arrangement.

    Now being part of this bigger Eclipse design has a few extra idiosyncrasies that we've had to deal with along the way as we added more features. One example is that Java code may execute from any number of triggers, such as user GUI events, external events (networking, other plugins triggering events, etc). This means that there are different Java Thread and ClassLoaders calling into plugin features that depends on j4rs. Long story short, the way I dealt with this was to modify at runtime the Application's SystemClassLoader to be the same ClassLoader that j4rs.jar is loaded into. This was a catch-all so that even when Threads originating in other plugins that were needing to use j4rs would always work, because they had a ClassLoader that could resolve j4rs classes.

    What has changed recently is the original Application vendor is starting to ship their Eclipse IDE-based program with updated JREs, and they are now using Java17. Unfortunately Java17 has begun to restrict some FFI-related functionality and also removed the ability to customize the SystemClassLoader. Of all of the approaches I could think of to fix this problem, they all required changes into the j4rs jar file (for example, the previous PR I submitted).

    I have observed two behaviors of Jvm::attach_thread() inside of the Eclipse program:

    • When the rust code is called by a Java Thread with a ClassLoader having j4rs via a Java native method, the attach_thread() acts as you would expect, and the existing JavaVM is used (as in https://github.com/astonbitecode/j4rs/blob/master/rust/src/api/mod.rs#L142), and when j4rs uses the Reflection API to invoke methods, everything works as planned.
    • When the rust code is called but the ClassLoader is missing j4rs.jar, the rust code can get a Jvm, but then j4rs will have exceptions for ClassNotFound for classes contained in j4rs.jar (such as NativeInstantiationImpl). I think this is fixable using the Thread ContextClassLoader as a back-up to the normal class resolution procedure. This behavior only manifests itself on edge-cases when certain triggers occur in the external plugins/incoming network requests.

    I think a fix for this latter case could be a new Jvm::attach_thread_with_classloader(), where the ClassLoader is provided beforehand, and a reference to it is stored in a Field in j4rs (Java code side), or persist an Instance in the rust side that isn't garbage collected. Another fix might be that a change to the Application-level configuration to load j4rs.jar at the Application level when the SCL isn't modifiable, but I do think Thread ContextClassLoaders were intended to fix this problem.

    Thank you for insights on this problem!

  3. astonbitecode commented on Sep 18, 2025

    @astonbitecode
    Owner

    Thanks for the explanation.

    j4rs needs a classloader in the following two contexts:

    1. To locate classes of the j4rs itself (classes inside the j4rs jar file)
    2. To locate classes needed by the user

    The first is a classloader that is used in the rust world. The native code uses this classloader to locate and execute jni calls to classes that are inside j4rs. When there is a pure native usage of j4rs (Rust -> Java direction), it is the classloader I talked about in my previous post and the JVM is created by j4rs itself using the JvmBuilder.

    However, you are interested in the Java -> Rust direction. In this case, the JVM is already created and the JvmBuilder cannot change many things in the already created JVM. I believe in your use case, not only you want to execute native Rust code, but you want your Rust code to further call Java code; the calling order being Java -> Rust -> Java.

    In the Rust side, the JNI calls for FindClass use the classloader provided by the JNIEnv (which is the one provided during the Java -> Rust call / thread). If this classloader does not have the j4rs classes inside, you will get the behavior you described in the second bullet in the above comment.

    Generally I would avoid on demand tweaking/changing the classloader used by j4rs. The correct thing to me, is to use the same classloader in both cases 1 and 2 described above. Since the current implementation that uses the system classloader to load classes does not work for your case, maybe it is worth to try changing this implementation to:

    return Class.forName(className);

    According to the javadoc:

    Invoking this method is equivalent to:
    Class.forName(className, true, currentLoader)
    where currentLoader denotes the defining class loader of the current class.

    Since the Java code that calls j4rs runs in a special plugin and classloader, then I suppose this is the most correct classloader to use, instead using the system classloader. Provided, of course, that j4rs jar is available in this classloader too.

    If the j4rs jar is not or cannot be in the classloader, any solution is a hack in my opinion.

    Can you try this small change and see if you get an acceptable result?

  4. astonbitecode commented on Mar 27, 2026

    @astonbitecode
    Owner

    Hi @toastertaster , do you have any updates on this? Can we close the issue?

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions