【问题标题】:jni4net failed to load DLLs in Java appjni4net 无法在 Java 应用程序中加载 DLL
【发布时间】:2014-06-27 09:36:10
【问题描述】:

我正在尝试修改 jni4net 示例代码 MyCSharpDemoCalc,并将该示例作为 .Net DLL 和 Java 层之间的桥梁。这是 C# 代码:

using System;
using Dynamsoft.DotNet.TWAIN;

namespace MyCSharpDemoCalc
{
    public interface ICalc
    {
        int MySuperSmartFunctionIDontHaveInJava(string question);
        bool IsShowUI();
    }

    public class DemoCalc : ICalc
    {
        private readonly Random r = new Random();
        private DynamicDotNetTwain dynamicDotNetTwain;

        public DemoCalc()
        {
            dynamicDotNetTwain = new Dynamsoft.DotNet.TWAIN.DynamicDotNetTwain();
        }

        public int MySuperSmartFunctionIDontHaveInJava(string question)
        {

            if (question == "Answer to the Ultimate Question of Life, the Universe, and Everything")
            {
                return 42;
            }
            return r.Next();
        }

        public bool IsShowUI()
        {
            return dynamicDotNetTwain.IfShowUI;
        }
    }
}

为了构建成功,我添加了以下引用:

  • System.Windows.Forms
  • DynamicDotNetTWAIN

然后输入命令

proxygen.exe MyCSharpDemoCalc.dll -wd work

生成MyCSharpDemoCalc.j4n.jarMyCSharpDemoCalc.j4n.dll

现在,我可以导入 DynamicDotNetTWAIN.dll、MyCSharpDemoCalc.j4n.dll、jni4net.n.w64.v20-0.8.6.0.dll、jni4net.n-0.8.6.0.dll、jni4net.j-0.8.6.0。 jar 和 MyCSharpDemoCalc.j4n.jar 到 Java 项目。

Java 代码:

import net.sf.jni4net.Bridge;

import java.io.IOException;

import mycsharpdemocalc.DemoCalc;
import mycsharpdemocalc.ICalc;

public class Program {
    public static void main(String arsg[]) throws IOException {
        Bridge.setClrVersion("v20");
        Bridge.init();
        Bridge.LoadAndRegisterAssemblyFrom(new java.io.File("DynamicDotNetTWAIN.dll"));
        Bridge.LoadAndRegisterAssemblyFrom(new java.io.File("MyCSharpDemoCalc.j4n.dll")); // crashed

        ICalc calc = new DemoCalc();
        final int result = calc.MySuperSmartFunctionIDontHaveInJava("Answer to the Ultimate Question of Life, the Universe, and Everything");

        System.out.printf("Answer to the Ultimate Question is : " + result);
        System.out.printf("If show UI : " + calc.IsShowUI());
    }
}

当我尝试运行应用程序时,它崩溃了

Bridge.LoadAndRegisterAssemblyFrom(新 java.io.File("MyCSharpDemoCalc.j4n.dll"));

Exception in thread "main" System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
   at System.Reflection.Module._GetTypesInternal(StackCrawlMark& stackMark)
   at System.Reflection.Assembly.GetTypes()
   at net.sf.jni4net.utils.Registry.RegisterAssembly(Assembly assembly, Boolean bindJVM, ClassLoader classLoader)
   at net.sf.jni4net.Bridge.RegisterAssembly(Assembly assembly, ClassLoader classLoader)
   at net.sf.jni4net.Bridge.LoadAndRegisterAssemblyFromClassLoader(File assemblyFile, ClassLoader classLoader)
   at net.sf.jni4net.Bridge.LoadAndRegisterAssemblyFrom(File assemblyFile)
   at net.sf.jni4net.__Bridge.LoadAndRegisterAssemblyFrom3(IntPtr __envp, JniLocalHandle __class, JniLocalHandle assemblyFile)
    at net.sf.jni4net.Bridge.LoadAndRegisterAssemblyFrom(Native Method)
    at com.main.Program.main(Program.java:68)

我该如何解决?谢谢!

【问题讨论】:

  • 嗨。你解决了这个问题吗?我也有同样的问题。
  • @Sergey 是的,我的代码现在可以运行了。我已将代码上传到Github

标签: java c# .net dll jni4net


【解决方案1】:

JNI4NET 将尝试从 JNI4NET 库所在的位置加载图像,唯一的解决方法 (AFAIK) 是将整个库复制到您的源目录,并使用复制的库编译您的包,然后它将工作.

【讨论】:

    猜你喜欢
    • 2015-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-06
    • 2012-02-14
    • 1970-01-01
    • 2020-07-16
    相关资源
    最近更新 更多