我想从c#代码中调用java API。我的Java API是一个绑定了几个第三方库的jar文件。我正在尝试使用IKVM和JNI4Net。我可以调用几个java函数,但是当代码依赖于第三方库时,它显示错误: NoClassDefFoundError‘在dll中发生。我的问题是,有没有可能使用这种基于JNI的工具从C#代码中执行java应用程序(依赖于许多第三方库)?
发布于 2015-05-08 16:20:34
虽然我不是jni4net专家,但我有一些这样做的经验。对于jni4net,您需要使用一个BridgeSetup并调用它的一个AddClassPath()方法来使用您的第三方库构建一个类路径。
例如,如下所示:
namespace My.Lib
{
using System;
using net.sf.jni4net;
public static class MyClass
{
public static void Init()
{
// create the setup and register the Java classpath
BridgeSetup bridgeSetup = new BridgeSetup(true);
bridgeSetup.AddClassPath("lib/myLib1.jar");
bridgeSetup.AddClassPath("lib/myLib2.jar");
// add others ...
// check the actual classpath we got
Console.WriteLine("registered classpath : ");
foreach (String s in bridgeSetup.JVMCLassPath) Console.WriteLine(s);
// now create the JVM
Bridge.CreateJVM(bridgeSetup);
}
}
}https://stackoverflow.com/questions/30101748
复制相似问题