【问题标题】:Need to reference and use a C# dll in Java需要在Java中引用和使用一个C# dll
【发布时间】:2018-04-09 05:54:37
【问题描述】:

我需要在 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) 任何帮助都会有所帮助!

【问题讨论】:

    标签: java c# .net dll jni4net


    【解决方案1】:

    我使用 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)

    希望对你有帮助

    【讨论】:

      【解决方案2】:

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

      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代码sn-p中调用:

      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 代码产生以下输出:

      来自 C# 的 Hello World! 2 + 3 = 5 和 sin(3,14159265) = 1,00000000

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

      希望它有用且清晰。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-05-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-08
        • 1970-01-01
        相关资源
        最近更新 更多