使用C#的jni4net与JVM连接时在webapp中出现错误

 2023-01-19    391  

问题描述

我正在尝试使用jni4net从我的C#WebApp内部访问简单的Java代码,但是它正在抛出一些错误.

生成了所有代理和DLL以访问Java类.

使用C#的jni4net与JVM连接时在webapp中出现错误

我编写了用于在’program.cs’文件中与JVM连接的代码.

稍后在自定义 Java函数 IE上. display_msg()从 testfunc()调用,可以使用 program.testfunc(). >

我正在附加program.cs文件和例外情况.
另外,我将我的java文件命名为test.java,它在包装中.

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.semblybuilder system.AppDomain.DefinedInedYnamicAssembly(System.Reflection.semblyName,System.Reflection.emit.emit.assemblyBuilderAccess)

)

以上所述是小编给大家介绍的使用C#的jni4net与JVM连接时在webapp中出现错误,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对77isp云服务器技术网的支持!

原文链接:https://77isp.com/post/25809.html

=========================================

https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。