1

我正在尝试使用 jni4net 从我的 C# webapp 内部访问一个简单的 java 代码,但它会引发一些错误。

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

我在“Program.cs”文件中编写了用于连接 JVM 的代码。

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

我附上了 Program.cs 文件和发生的异常。此外,我将我的 java 文件命名为 Test.java,它位于 mypack 包中。

程序.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();
        }

    }
}

测试.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# 的初学者,所以请帮我解决这个问题。

4

1 回答 1

1

从评论中我们推断出.NET core 2.1 不支持这种方法:(也许还有其他)

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

于 2019-09-30T10:01:31.463 回答