如何使用 .Net Maui 的 Windows 平台来运行 IKVM 并在运行时读取和运行 jar 文件?

huangapple go评论88阅读模式
英文:

How to use .Net Maui windows platform to run IKVM and read and run jar file on the run?

问题

我有一个Java库项目,它被编写为另一个Java项目的库。在库项目内有数百个不同类别的类,每个类都提供不同类型的方法,但它们都继承自一个父类。

Java库中的每个类都有一个构造函数,用于为该类本身定义一些独特的值。

现在,我想开发一个C#应用程序,最好使用.NET Maui,以便我可以读取库的jar文件并执行每个子类的构造函数,然后读取这些独特的值。独特的值字段在父类中定义为受保护字段,但父类中有一个公共的getter可以用来检索该独特的值。

jar文件将由其他人使用JDK 1.8.0进行编译。

请注意,会有许多jar文件,因为不同的产品将使用不同的Java库,因此我需要允许用户在运行时从文件选择器或类似的东西中输入jar文件到我的Maui项目中。

我的目标是从我的Maui项目中可以输入任何jar文件,检查所有可用的类,执行它们的构造函数,然后执行获取独特值的getter,并获取每个类的独特值。

我对Java不太熟悉,但对C#相当熟悉。这是否可能,是否有任何我可以使用的示例语法?我已经搜索和尝试了几天,但没有成功,因为缺乏关于C#语法的参考。

提前感谢您。

英文:

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.

答案1

得分: 1

以下是翻译好的代码部分:

// 首先,在包管理器控制台上运行 `install-package` 安装 IKVM。
install-package IKVM

// 然后,使用 Blazor 文件选择器选择一个文件,并使用以下语法将类加载到 Java 中。
var result = await FilePicker.Default.PickAsync();
string URL = result.FullPath;
Java.net.ClassLoader classloader = new Java.net.URLClassLoader(new URL[]{new URL("file:///" + URL)});

// 从这里可以了解如何从类加载器加载类,以及在创建类之后如何创建类的实例。
var ClassIwantToUse = classLoader.loadClass("com.company.name");
Object classObject = ClassIwantToUse.newInstance();

// 从这里,您还可以获取该类的已声明方法,并通过在调用构造函数时提供在实例化对象时创建的对象来调用该方法。
var theMethod = ClassIwantTouse.getMethod("getPublicUniqueValue");
var methodresult = theMethod.Invoke(classObject);

希望对您有所帮助!如果您有任何问题,请随时提问。

英文:

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

huangapple
  • 本文由 发表于 2023年2月19日 12:17:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75497957.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定