使用来自C#的jni4net与JVM连接时,webapp中出现错误 [英] Error in the webapp while connecting with JVM using jni4net from C#

查看:164
本文介绍了使用来自C#的jni4net与JVM连接时,webapp中出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用jni4net从我的C#Web应用程序内部访问一个简单的Java代码,但这会引发一些错误.

I'm trying to access a simple java code from inside my C# webapp using jni4net, but it is throwing some errors.

生成所有代理和dll来访问java类.

Generated all proxies and dlls to access the java class.

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

I wrote the code for connecting with JVM inside the 'Program.cs' file.

有关自定义 java函数的最新信息,即从 testfunc()调用 display_msg(),可以使用 Program.testfunc()在机器人内部的任何位置调用.

Later on the custom java function ie. display_msg() is called from the testfunc() which can be called from anywhere inside the bot using Program.testfunc().

我正在附加Program.cs文件,并且发生了异常. 另外,我已将Java文件命名为Test.java,它位于包mypack中.

I'm attaching the Program.cs file and the exception occurring. Also I've named my java file as Test.java and it's inside package mypack.

Program.cs

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

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#的初学者,所以请帮助我.

I'm a beginner to C# so please help me out with this.

推荐答案

从注释中我们推断出.NET Core 2.1不支持此方法:(可能还有其他方法)

From the comments we deduced that .NET core 2.1 does not support this method: (and maybe others)

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

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

这篇关于使用来自C#的jni4net与JVM连接时,webapp中出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆