-
Notifications
You must be signed in to change notification settings - Fork 56
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
[Java.Interop] Use Class.forName()
as fallback to load Java classes
#1326
Open
jonpryor
wants to merge
2
commits into
main
Choose a base branch
from
dev/jonp/jonp-Class.forName
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+18
−23
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Fixes: #23 Context: dotnet/android@aba2726 Context: b4d44e4 Context: xamarin/monodroid@ed984a3 Context: dotnet/android#7616 Commit b4d44e4 noted: > Android is..."special", in that not all threads get the same > ClassLoader behavior. Specifically, *managed* threads -- > System.Threading.Thread instances -- get a different ClassLoader than > the main/UI thread on Android. (Untested, but the ClassLoader *may* > behave sanely if you use a java.lang.Thread instance instead. But who > wants to use java.lang.Thread instances...?) dotnet/android#7616 provided additional context: `JNIEnv::FindClass()` behavior *is* tied to the thread that calls it, and one of the knock-on effects is that `Java.Lang.JavaSystem.LoadLibrary("MyLib")` doesn't work properly when invoked from a new `System.Threading.Thread` thread. Which brings us to xamarin/mondroid@ed984a3a, which updated then Xamarin.Android to use [`java.lang.Class.forName(String)`][0] instead of [`ClassLoader.loadClass(String)`][1] because the "real" JDK cannot use `ClassLoader.loadClass(String)` to load array types: Class c1 = Class.forName("[Ljava.lang.String;"); // works Class c2 = ClassLoader.getSystemClassLoader() .loadClass("[Ljava.lang.String;"); // throws java.lang.ClassNotFoundException Class c3 = Class.forName("[I"); // works; array of int Class c4 = ClassLoader.getSystemClassLoader() .loadClass("[I"); // throws java.lang.ClassNotFoundException Using `ClassLoader.loadClass(String)` to load array types works on Android, presumably as an undocumented implementation detail, but as xamarin/monodroid@ed984a3a was trying to get things working within the (now dead) Android Designer -- which ran using a Desktop JDK -- Android-specific extensions were not available. Update `JniEnvironment.Types` to use `Class.forName(String)` instead of `ClassLoader.loadClass(String)` to load Java classes, as a fallback for when `JNIEnv::FindClass()` fails to find the class. [0]: https://developer.android.com/reference/java/lang/Class#forName(java.lang.String) [1]: https://developer.android.com/reference/java/lang/ClassLoader#loadClass(java.lang.String)
jonpryor
added a commit
to dotnet/android
that referenced
this pull request
Mar 19, 2025
Context: dotnet/java-interop#1326 Does It Build™?
Context: dotnet/android#9931 Context: dotnet/android#9931 (comment) The attempted integration test crashed! F mono-rt : [ERROR] FATAL UNHANDLED EXCEPTION: Java.Lang.ClassNotFoundException: Didn't find class "crc641855b07eca6dcc03.MyCb" on path: DexPathList[[directory "."],nativeLibraryDirectories=[/system/lib64, /system/product/lib64, /system/lib64, /system/product/lib64]] F mono-rt : at Java.Interop.JniEnvironment.Types.TryFindClass(String classname, Boolean throwOnError) F mono-rt : at Java.Interop.JniEnvironment.Types.FindClass(String classname) F mono-rt : at Java.Interop.JniType..ctor(String classname) F mono-rt : at Java.Interop.JniPeerMembers.JniInstanceMethods..ctor(Type declaringType) F mono-rt : at Java.Interop.JniPeerMembers.JniInstanceMethods.GetConstructorsForType(Type declaringType) F mono-rt : at Java.Interop.JniPeerMembers.JniInstanceMethods.StartCreateInstance(String constructorSignature, Type declaringType, JniArgumentValue* parameters) F mono-rt : at Java.Lang.Object..ctor() F mono-rt : at Java.InteropTests.MyCb..ctor() F mono-rt : at Java.InteropTests.JnienvTest.<>c__DisplayClass29_0.<DoNotLeakWeakReferences>b__1() F mono-rt : at System.Threading.Thread.StartHelper.Callback(Object state) F mono-rt : at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) F mono-rt : --- End of stack trace from previous location --- F mono-rt : at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) F mono-rt : --- End of managed Java.Lang.ClassNotFoundException stack trace --- F mono-rt : java.lang.ClassNotFoundException: Didn't find class "crc641855b07eca6dcc03.MyCb" on path: DexPathList[[directory "."],nativeLibraryDirectories=[/system/lib64, /system/product/lib64, /system/lib64, /system/product/lib64]] F mono-rt : at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:196) F mono-rt : at java.lang.ClassLoader.loadClass(ClassLoader.java:379) F mono-rt : at java.lang.ClassLoader.loadClass(ClassLoader.java:312) Perhaps unsurprisingly, we need to provide the system ClassLoader in order to load types from outside the default "classpath", which is why we used `ClassLoader.loadClass()` in the first place. Instead of `Class.forName()`, use the [`Class.forName(String name, boolean initialize, ClassLoader loader)`][0] overload, so that we can provide a `ClassLoader` instance. This *should* fix the crash. [0]: https://developer.android.com/reference/java/lang/Class#forName(java.lang.String,%20boolean,%20java.lang.ClassLoader)
jpobst
approved these changes
Mar 20, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems perfectly cromulent.
🫡 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes: #23
Context: dotnet/android@aba2726
Context: b4d44e4
Context: https://github.com/xamarin/monodroid/commit/ed984a3a0bfbe71f6499a7855c647b5bc2b28466
Context: dotnet/android#7616
Commit b4d44e4 noted:
dotnet/android#7616 provided additional context:
JNIEnv::FindClass()
behavior is tied to the thread that calls it, and one of the knock-on effects is thatJava.Lang.JavaSystem.LoadLibrary("MyLib")
doesn't work properly when invoked from a newSystem.Threading.Thread
thread.Which brings us to xamarin/mondroid@ed984a3a, which updated then Xamarin.Android to use
java.lang.Class.forName(String)
instead ofClassLoader.loadClass(String)
because the "real" JDK cannot useClassLoader.loadClass(String)
to load array types:Using
ClassLoader.loadClass(String)
to load array types works on Android, presumably as an undocumented implementation detail, but as xamarin/monodroid@ed984a3a was trying to get things working within the (now dead) Android Designer -- which ran using a Desktop JDK -- Android-specific extensions were not available.Update
JniEnvironment.Types
to useClass.forName(String)
instead ofClassLoader.loadClass(String)
to load Java classes, as a fallback for whenJNIEnv::FindClass()
fails to find the class.