1

我想从 c# 代码调用 java API。我的 Java API 是一个与多个 3rd 方库捆绑在一起的 jar 文件。我正在尝试使用 IKVM 和 JNI4Net。我可以调用几个 java 函数,但是当代码依赖于 3rd 方库时,它会显示错误:NoClassDefFoundError'发生在 dll 中。我的问题是可以使用此类基于 JNI 的工具从 C# 代码执行 java 应用程序(依赖于许多 3rd 方库)吗?

4

1 回答 1

0

如果不是 jni4net 专家,我在这方面有一些经验。使用 jni4net,您需要使用 BridgeSetup 并调用其AddClassPath()方法之一来使用您的 3rd-party 库构建类路径。

例如,像这样:

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);
    }
  }
}
于 2015-05-08T08:20:34.043 回答