0

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.

Fitri Halim
  • 584
  • 4
  • 11
  • Sorry, but I don’t think MAUI. allows you to do that. MAUI allows your C# application to run on any platform - basically it installs a .NET CLR on each platform, so your C# application only sees .NET. However, that. .NET CLR will only execute .NET bytecode - there will be no “magic backdoor” for the CLR to also execute Java bytecode (nor therefore to interpret/execute jar files) – racraman Feb 19 '23 at 08:10
  • @racraman, how about other alternative such as winform or MVC, is it possible to do that? – Fitri Halim Feb 19 '23 at 10:04
  • Nope - C# cannot execute JVM code; they are different beasts. What you could do is translate your Java libraries to C# - there are tools around to help - see https://stackoverflow.com/questions/896867/tool-to-convert-java-to-c-sharp-code – racraman Feb 20 '23 at 04:08
  • @racraman Actually after trying and trying, I already managed to do it using IKVM. I will share my approach as the answer later. Thanks by the way – Fitri Halim Feb 20 '23 at 04:14

1 Answers1

1

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

Fitri Halim
  • 584
  • 4
  • 11