2

我需要在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 执行相同的操作,但没有成功。我正在使用 jni4net-0.8.8.0 版本和 Eclipse IDE for Java Developers 版本:Oxygen.3 Release (4.7.3) 任何帮助都会有所帮助!

4

2 回答 2

0

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

希望这会有所帮助

于 2018-04-23T17:02:12.573 回答
0

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

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 代码段调用上一个类的方法:

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# 的你好世界!!2 + 3 = 5 和 sin(3,14159265) = 1,00000000

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

希望它有用且清晰。

于 2020-04-18T20:35:12.920 回答