I have a java library project that is written as a library for another java project. Inside that library project there are hundreds of classes each of them supplying different kind of methods , but each if them is inheriting from a parent class.
Each of the class inside the java Library has a constructor that define some unique value for the class itself.
Now I want develope a C# application preferably using .Net Maui, so that I can read the library jar file and execute the constructer for each of the child class, and read those unique value. The unique value field are defined in the parent class as a protected field, but there is a public getter in the parent class that can be used to retrieve that unique value.
The jar file will be compiled using JDK 1.8.0 by other people.
Noted that there will be many jar files because different products will have different java library, so I need to allow user to input the jar file into my Maui project during runtime from filepicker or something similar.
The endpoint is that from my Maui project I can input any jar file, check all available classes, execute their constructor, then execute the getter for the unique value and get what is their unique value for each class.
I am not very familiar with Java, but quite good with C#. Is this is possible, and is there any sample syntax that i can use for? I had been searching and trying for few days without any success, since there are lacks of reference on the Syntax in C#.
Thank you in advance.
After trying for few days, the answer to this question is rather simple and straightforward as per mentioned by the this Reddit page https://www.google.com/url?sa=t&source=web&rct=j&url=https://www.reddit.com/r/dotnet/comments/vqqpqb/ikvm_820/&ved=2ahUKEwiOwazRnqX9AhXkJrcAHeXKAXAQjjh6BAgJEAE&usg=AOvVaw3YoS8m3GSuBmkUrtOPsTNv .
First, you run install-package
IKVM on package manager console.
Secondly, use a blazor File Picker to pick a file, and load the class using the URL into java using this syntax
var result = await FilePicker.Default.PickAsync();
string URL = result.FullPath;
Java.net.ClassLoader classloader = new Java.net.URLClassLoader(new URL[]{new URL("file:///" + URL)});
From here you can read more about how to load class from a class loader, and how to create the instance of the class after you create it. When we create the instance of the class, the default constructor will automatically executed.
var ClassIwantToUse = classLoader.loadClass("com.company.name");
Object classObject = ClassIwantToUse.newInstance();
From there, you can also get declared method from that class, and invoke that method by supplying the object created when you instantiate the object when you call the constructor previously.
var theMethod = ClassIwantTouse.getMethod("getPublicUniqueValue")
var methodresult = theMethod.Invoke(classObject);
Feel free to drop any commend if you have any questions. Thank you