Java的JNI在64位和32位本机库加载,但不会在32位运行(Java JNI native li

2019-10-30 11:14发布

我有一个JNI的应用程序它是Windows 7 64位我的主电脑上工作,但它不能在我的笔记本电脑它是Windows XP的32位工作。 在笔记本电脑上,本机加载DLL的32位版本没有UnsatisfiedLinkError ,但我得到当实际本地方法被称为错误。 这里是我用MinGW的编译我的DLL文件:

cd bin
javah MyClass
move /Y MyClass.h ../
"%mingw64%\bin\g++" -shared -I"C:\Program Files\Java\jdk1.8.0_05\include" -I"C:\Program Files\Java\jdk1.8.0_05\include\win32" -o src/lib/MyLibrary.dll MyClass.cpp
@echo "Making the 32-bit library..."
g++ -shared -I"C:\Program Files\Java\jdk1.8.0_05\include" -I"C:\Program Files\Java\jdk1.8.0_05\include\win32" -o src/lib/MyLibrary_32.dll MyClass.cpp

这里是我用来加载库,这在技术上适用于两个处理器的代码。

    String processor = System.getProperty("sun.arch.data.model").equals("32") ? new String("_32") : new String("");
    try { System.load(System.getProperty("user.dir") + "\\MyLibrary" + processor + ".dll"); }
    catch(UnsatisfiedLinkError e) {
        //If the dll was not found, extract the one packaged as a resource in the .jar
        InputStream in = ML_Overworld.class.getClassLoader().getResourceAsStream("lib/MyLibrary" + processor + ".dll");
        if(in == null) {
            printError(String.format("MyLibrary%s.dll was not contained in the jar.", processor));
            return;
        }
        File outPath = new File(System.getProperty("user.dir") + "\\MyLibrary" + processor + ".dll");
        try {
            DataOutputStream out = new DataOutputStream(new FileOutputStream(outPath));
            try {
                int input;
                while((input = in.read()) != -1)
                    out.write(input);
                out.close();
                in.close();
            }
            catch(IOException e2) {
                printError(String.format("MyLibrary%s.dll failed to extract: ", processor) + e2.getMessage() + ".");
                return;
            }
        }
        catch(FileNotFoundException e3) {
            printError(String.format("MyLibrary%s.dll failed to extract: ", processor) + e3.getMessage() + ".");
            return;
        }
        try { System.load(System.getProperty("user.dir") + "\\MyLibrary" + processor + ".dll"); }
        catch(UnsatisfiedLinkError e4) {
            printError(String.format("MyLibrary%s.dll failed to load: ", processor) + e4.getMessage() + ".");
            return;
        }
    }

和作为参考,在此可以将引发本地方法Unsatisfied Link Error的32位膝上型: public static native void readMap(String path, cMap m);


[溶液]:加入--kill-at对于g ++参数编译32位DLL时。

文章来源: Java JNI native library loads on 64bit and 32bit, but doesn't run on 32bit