简体   繁体   中英

Error using Java Native Interface

I'm trying to develop a java program that uses c++ code. For this purpose, I'm using a 32-bit mingw, a 32-bits jre (version 1.8.0_77) and eclipse luna (64 bits) .

The problem

I have problems trying to load the *.dll. (I haven't tried to run any method yet)

What I have tested

This is the class where I have declared the native method:

public class JniImports {
    static{
        System.loadLibrary("hello");
    }

    public int test(int n){
        return testJava(n);
    }

    private native int testJava(int n);
}

And this is the Main method:

public class Main {
    public static void main(String[] args) {
        JniImports a = new JniImports();
    }
}

I have configured the arguments for the JVM on eclipse adding this: -Djava.library.path=./jni

The c-header file has been generated by the javah tool, so I'm sure that's ok. Anyway, as I have said, I have not tried to execute any method. Here the header file:

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_jnitest_JniImports */

#ifndef _Included_com_jnitest_JniImports
#define _Included_com_jnitest_JniImports
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_jnitest_JniImports
 * Method:    testJava
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_com_jnitest_JniImports_testJava
  (JNIEnv *, jobject, jint);

#ifdef __cplusplus
}
#endif
#endif

And here the cpp file:

#include "com_jnitest_JniImports.h"

JNIEXPORT jint JNICALL Java_com_jnitest_JniImports_testJava
  (JNIEnv *env, jobject _this, jint n){
      return 12;
}

Here is the command that I'm using to compile (g++ is mingw 32-bits): g++ -Wl,--add-stdcall-alias -I"%JDK32_HOME%\\include" -I"%JDK32_HOME%\\include\\win32" -shared -o hello.dll com_jnitest_JniImports.cpp

The errors

If I use a 64-bits JRE, the error is that it can't load a 32-bit dll on a 64-bit jre:

java.lang.UnsatisfiedLinkError: D:\\ws\\testjni\\jni\\hello.dll Can't load IA 32-bit .dll on a AMD 64-bit platform

I think this error shows that, in this case, the JRE finds the *.dll.

If I use a 32-bits JRE, the error is that it can't find the *.dll:

java.lang.UnsatisfiedLinkError: D:\\ws\\testjni\\jni\\hello.dll: Can't find dependent libraries

If I use a 32-bits JRE and I remove the hello.dll library, this is the error: java.lang.UnsatisfiedLinkError: no hello in java.library.path

So, compiling the c++ code with a 32-bit compiler and using a 32-bit JRE, it can find the dll, but it crashes anyway.

I have also tried to compile the c++ code in debug mode (with -g flag) and it also crashes.

I'm sure that the dll is in the correct place, because if I copy-paste the path displayed in the error log in the window's explorer path bar, it takes me to the folder where the dll is, and I'm sure that its name is "hello.dll".

I have tested it with the last version of eclipse (32 bits) and it works. I don't know if it was an eclipse bug, or if the problem was that I was using a 64-bit eclipse and now I'm using A 32-bit one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM