1

我正在使用 JNI4net,虽然库和安装在构建路径中并且 Eclipse 可以识别它们,但它仍然给我运行时错误。为什么你会这么认为?这是代码。

 import net.sf.jni4net.*;

import java.io.IOException;
import java.lang.String;

import system.*;
import system.Object;
import system.io.TextWriter;
import system.collections.IDictionary;
import system.collections.IEnumerator;

    /**
     * @author Pavel Savara (original)
     */
    public class Program {
        public static void main(String[] args) throws IOException {
            // create bridge, with default setup
            // it will lookup jni4net.n.dll next to jni4net.j.jar 
            //Bridge.setVerbose(true);
            Bridge.setVerbose(true);
            Bridge.init();

            // here you go!
            Console.WriteLine("Hello .NET world!\n");

            // OK, simple hello is boring, let's play with System.Environment
            // they are Hashtable realy
            final IDictionary variables = system.Environment.GetEnvironmentVariables();

            // let's enumerate all keys
            final IEnumerator keys = variables.getKeys().GetEnumerator();
            while (keys.MoveNext()) {
                // there hash table is not generic and returns system.Object
                // but we know is should be system.String, so we could cast
                final system.String key = (system.String) keys.getCurrent();
                Console.Write(key);

                // this is automatic conversion of JVM string to system.String
                Console.Write(" : ");

                // we use the hashtable
                Object value = variables.getItem(key);

                // and this is JVM toString() redirected to CLR ToString() method
                String valueToString = value.toString();
                Console.WriteLine(valueToString);
            }

            // Console output is really TextWriter on stream
            final TextWriter writer = Console.getOut();
            writer.Flush();
        }
    }

这是我收到的信息!

    Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    Bridge cannot be resolved
    Bridge cannot be resolved
    Console cannot be resolved
    IDictionary cannot be resolved to a type
    system cannot be resolved
    IEnumerator cannot be resolved to a type
    system cannot be resolved to a type
    system cannot be resolved to a type
    Console cannot be resolved
    Console cannot be resolved
    Console cannot be resolved
    TextWriter cannot be resolved to a type
    Console cannot be resolved

    at Program.main(Program.java:37)
4

1 回答 1

1

为了让您的生活更轻松,我将在这里分享我的发现。阅读 Martin Serrano 对我的问题的回答。它将帮助您了解需要做什么。然后访问 jni4net 的网站并下载他们的示例 zip 文件夹。提取它。那里有一个名为 myCSharpDemoCalc 的示例。用 myCSharpDemoCalc.dll(在工作文件夹内)替换您的 dll,然后运行 ​​generateProxies.cmd(确保将此文件编辑为您的 dll 名称)并运行.cmd。然后转到工作文件夹并运行 build.cmd(编辑名称)来创建您的 JAR 文件。它可能不会吐出你可能需要自己调整路径的 j4n.dll。使用这个 JAR 文件。对于我来说,这是从第三方 dll 创建 JAR 文件的最简单方法。

于 2015-03-02T22:35:51.243 回答