0

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(new 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)

どうすれば修正できますか?ありがとう!

4

1 に答える 1

1

JNI4NET は、JNI4NET ライブラリが置かれていた場所からイメージをロードしようとします。唯一の回避策 (AFAIK) は、ライブラリ全体をソース ディレクトリにコピーし、コピーしたライブラリを使用してパッケージをコンパイルすることでした。

于 2014-07-25T08:59:00.250 に答える