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

喜夏-厌秋 提交于 2019-12-24 08:08:55

问题


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

Generated all proxies and dlls to access the java class.

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

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().

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

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

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.

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


回答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)



来源:https://stackoverflow.com/questions/57866038/error-in-the-webapp-while-connecting-with-jvm-using-jni4net-from-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!