You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Additions to j4rs rust-side Jvm to support custom thread classloader configuration #150
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 withlet 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.
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?
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 ThreadContextClassLoader 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.
j4rs needs a classloader in the following two contexts:
To locate classes of the j4rs itself (classes inside the j4rs jar file)
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:
returnClass.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?
Activity
astonbitecode commentedon Sep 16, 2025
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 optionjava.system.class.loader. This is why we may add classes to the classpath at any time, even after the initialization (using theJvm.deploy_artifact).In case you want a different classloader you could for example initialize the Jvm like:
This way, the
my.OtherClassloaderwill 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?
toastertaster commentedon Sep 18, 2025
I have been using
j4rsinside of an Eclipse IDE plugin. Eclipse-IDE-based programs set up a sophisticated Java program design with a main program appClassLoader, and then plugins can configure to have their ownClassLoaderhierarchy 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. Thej4rs-with-dependencies.jaris made available on theClassLoaderfor my plugin. Usingj4rsthis way has worked very well without needing to modify the original application configuration files, and we've been usingj4rsfor 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
ThreadandClassLoaderscalling into plugin features that depends onj4rs. Long story short, the way I dealt with this was to modify at runtime the Application'sSystemClassLoaderto be the sameClassLoaderthatj4rs.jaris loaded into. This was a catch-all so that even when Threads originating in other plugins that were needing to usej4rswould always work, because they had a ClassLoader that could resolvej4rsclasses.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 thej4rsjar file (for example, the previous PR I submitted).I have observed two behaviors of
Jvm::attach_thread()inside of the Eclipse program:j4rsvia a Javanativemethod, theattach_thread()acts as you would expect, and the existingJavaVMis used (as in https://github.com/astonbitecode/j4rs/blob/master/rust/src/api/mod.rs#L142), and whenj4rsuses the Reflection API to invoke methods, everything works as planned.j4rs.jar, the rust code can get aJvm, but then j4rs will have exceptions for ClassNotFound for classes contained inj4rs.jar(such asNativeInstantiationImpl). I think this is fixable using theThreadContextClassLoaderas 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 aFieldinj4rs(Java code side), or persist anInstancein the rust side that isn't garbage collected. Another fix might be that a change to the Application-level configuration to loadj4rs.jarat 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!
astonbitecode commentedon Sep 18, 2025
Thanks for the explanation.
j4rsneeds a classloader in the following two contexts: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
j4rsitself using theJvmBuilder.However, you are interested in the Java -> Rust direction. In this case, the JVM is already created and the
JvmBuildercannot 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
FindClassuse the classloader provided by theJNIEnv(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:
According to the javadoc:
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?
astonbitecode commentedon Mar 27, 2026
Hi @toastertaster , do you have any updates on this? Can we close the issue?