使用 C# 中的 jni4net 连接 JVM 时 webapp 出错

Error in the webapp while connecting with JVM using jni4net from C#

我正在尝试使用 jni4net 从我的 C# webapp 中访问一个简单的 java 代码,但它抛出了一些错误。

已生成所有代理和 dll 以访问 java class。

我在 'Program.cs' 文件中编写了连接 JVM 的代码。

稍后自定义 java 函数 即。 display_msg()testfunc() 调用,可以使用 [ 从机器人内部的任何地方调用它=40=]().

我附上了 Program.cs 文件并且发生了异常。 此外,我将我的 java 文件命名为 Test.java 并且它在包 mypack.

Program.cs

using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Logging;
using net.sf.jni4net;
using System;
using mypack;

namespace ValidationBot
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var setup = new BridgeSetup();
            setup.Verbose = true;
            setup.AddAllJarsClassPath("./");
            Bridge.CreateJVM(setup);
            Bridge.RegisterAssembly(typeof(Test).Assembly);
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
            .ConfigureLogging((logging) =>
            {
                logging.AddDebug();
                logging.AddConsole();
            }).UseStartup<Startup>();

        public static void testfunc()
        {
            Test test = new Test();
            test.display_msg();

            Console.WriteLine("\nPress any key to quit.");
            Console.ReadKey();
        }

    }
}

Test.java

package mypack;
import java.io.*;

public class Test
{
  public static void main(String args[])
  {
     int s =10;
     System.out.println(s);
  }
  public void display_msg()
  {
     System.out.println("Hello, I'm inside a java program");
  }
}

异常

Exception thrown: 'System.MissingMethodException' in jni4net.n-0.8.8.0.dll
Exception thrown: 'System.Reflection.TargetInvocationException' in System.Private.CoreLib.dll
Exception thrown: 'System.TypeInitializationException' in jni4net.n-0.8.8.0.dll
An unhandled exception of type 'System.TypeInitializationException' occurred in jni4net.n-0.8.8.0.dll
The type initializer for 'net.sf.jni4net.utils.Registry' threw an exception.

我是 C# 的初学者,所以请帮我解决这个问题。

从评论中我们推断 .NET core 2.1 不支持此方法:(可能还有其他方法)

System.Reflection.Emit.AssemblyBuilder System.AppDomain.DefineDynamicAssembly(System.Reflection.AssemblyName, System.Reflection.Emit.AssemblyBuilderAccess)