我正在尝试使用 jni4net 从我的 C# web 应用程序内部访问简单的 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# 初学者,所以请帮助我解决这个问题。
最佳答案
从评论中我们推断 .NET core 2.1 不支持此方法:(也许还有其他方法)
System.Reflection.Emit.AssemblyBuilder System.AppDomain.DefineDynamicAssembly(System.Reflection.AssemblyName, System.Reflection.Emit.AssemblyBuilderAccess)
关于java - 使用 C# 中的 jni4net 连接 JVM 时 Web 应用程序出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57866038/