需要在 Java 中引用和使用 C# dll

Need to reference and use a C# dll in Java

我需要在 java 中引用一个 .Net dll。我同样使用了 jni4net 库。我已按照以下视频中提到的步骤进行操作:

https://www.youtube.com/watch?time_continue=351&v=8OoSK_RWUe4

我已按照引用 jni4net 库所需的所有步骤进行操作,但出现以下运行时异常:

Exception in thread "main" java.lang.UnsatisfiedLinkError: orionforpython.DynamicOrion.__ctorDynamicOrion0(Lnet/sf/jni4net/inj/IClrProxy;)V
at orionforpython.DynamicOrion.__ctorDynamicOrion0(Native Method)
at orionforpython.DynamicOrion.<init>(DynamicOrion.java:25)
at com.orion.OrionForJava.main(OrionForJava.java:16)

完成所有步骤后,这是我的代码:

    package com.orion;
    import net.sf.jni4net.Bridge;
    import orionforpython.*;
    import java.io.*;
    class OrionForJava {
    public static void main(String[] args) throws IOException {
    Bridge.setVerbose(true);
    Bridge.init();
    File proxyAssemblyFile=new File("OrionForPython.dll");
    Bridge.LoadAndRegisterAssemblyFrom(proxyAssemblyFile);
    DynamicOrion orion=new DynamicOrion();
    String res=orion.ReqLogin("user", "pwd", "");
    System.out.print(res);
  }}

我尝试使用 NetBeans 8.1 IDE 执行相同的操作,但没有成功。我正在为 Java 开发人员使用 jni4net-0.8.8.0 版本和 Eclipse IDE 版本:Oxygen.3 发布 (4.7.3) 任何帮助都会有所帮助!

我使用 jni4net 库从 java 调用 c# dll,它工作正常。我使用了一种稍微不同的方法来初始化 jni4net。

try {
        Bridge.setVerbose(true);
        Bridge.init(new File("Full path to jni4net.n.w64.v40-0.8.8.0.dll"));
        // where dlls to load is jni4net.n.w64.v40-0.8.8.0.dll,jni4net.n-0.8.8.0.dll,MyOriginalNETDll.dll,MyOriginalNETDll.j4n.dll (after proxygen processing)
        for (String str : dllsToLoad) {
            File dll = new File(rutaDlls + str);
            Bridge.LoadAndRegisterAssemblyFrom(dll);
        }
    } catch (IOException e) {
        LOG.error("Error jniBrige.", e);
    }

我需要使用完整路径 c:... 到 dll 才能使其正常工作。我还必须注意用于创建程序集的 .net 框架版本(在我的情况下需要使用 4.0 和 java 版本 8)

希望对您有所帮助

我们使用 JCOBridge,它可以在 .NET Core (>= 3.1) 和 .NET Framework (>= 4.6.1) 中使用。引用您需要调用的 DLL,您将拥有对它的完全访问权限,并且可以在您的项目中使用它。 考虑以下 C# 片段 class 在通用 TestBridge.dll 中可用:

using System;

namespace TestBridge
{
    public class MyClass
    {
        /// <summary>The method <c>HelloWorld</c> return the "Hello World!!" string</summary>
        public String HelloWorld()
        {
            return "Hello World from C#!!";
        }

        /// <summary>The method <c>Add</c> return the sum of two double</summary>
        public double Add(double a, double b)
        {
            return a + b;
        }

        /// <summary>The method <c>Add</c> return the sin of a double</summary>
        public double Sin(double a)
        {
            return Math.Sin(a);
        }
    }
}

可以从以下 java 代码片段调用前面 class 的方法:

import java.io.IOException;
import org.mases.jcobridge.*;

public class CSharpClassUse {

  public static void main(String[] args) {
    try {
      try {
        try {
          JCOBridge.Initialize();
          } catch (JCException e) {
            e.printStackTrace();
          }
        } catch (IOException e) {
          e.printStackTrace();
          return;
        }
        //declare and create JCOBridge instance
        JCOBridge bridge;
        bridge = JCOBridge.CreateNew();
        // adds the path where external assemblies can be found
        bridge.AddPath("Path where is TestBridge.dll assembly");
        // add REFERENCES to the .dll file you want to invoke
        bridge.AddReference("TestBridge.dll");
        // INSTANTIATE the .NET Object: JCObject is a meta object
        JCObject theNetClassInstance = (JCObject) bridge.NewObject("TestBridge.MyClass");
        double a = 2;
              double b = 3;
              double c = Math.PI/2;
              //Invoke the C# class methods
              String hello = (String) theNetClassInstance.Invoke("HelloWorld");
              double result = (double) theNetClassInstance.Invoke("Add", a, b);
              double sin = (double) theNetClassInstance.Invoke("Sin", c);
              System.out.println(String.format("%s %.0f + %.0f = %.0f and sin(%.8f) = %.8f", hello, a, b, result, c, sin));
      } catch (JCException jce) {
      jce.printStackTrace();
      System.out.println("Exiting");
      return;
    }
  }
}

前面的 java 代码产生以下输出:

Hello World from C#!! 2 + 3 = 5 and sin(3,14159265) = 1,00000000

前面的示例展示了如何使用 DLL 中可用的 C# class。如果您需要 invoke/integrate .NET 图形,一般意义上也是 C# DLL,JCOBridge 还管理 GUI 集成(WPF/WinForms/AWT/Swing):查看这些 Examples

希望它有用且清晰。