0

I have the following Java native function

private static native void fillEnergyArrays(double currentTemp, double concNA, double concMG);

The function is implemented in C. The code has been tested and I have created a working dynamic library, libhyfiModel.jnilib, which I can use to call the method from a Java file, Temp.java. There are no problems in this case when I run the program with the one file. The method call works and returns the expected value.

The problem:

I am taking Temp.java and libhyfiModel.jnilib and trying to import them into a large IntelliJ IDEA project which uses Maven as a build manager. I have put my libhyfiModel.jnilib file in my correct directory so it loaded when the program executes. I double checked that it's loaded using this code in Temp.java:

static {
    try {
        System.loadLibrary("hyfiModel");
    }
    catch (UnsatisfiedLinkError e) {
        System.err.println("Native code library failed to load\n" + e);
    }
}

No exception is thrown here, so libhyfiModel.jnilib is found and loaded correctly, but when the native method is executed I get the following error:

Exception in thread "main" java.lang.UnsatisfiedLinkError: ca.virology.baseByBase.DiffEditor.fillEnergyArrays(DDD)V at ca.virology.baseByBase.DiffEditor.fillEnergyArrays(Native Method) at ca.virology.baseByBase.DiffEditor.main(DiffEditor.java:266)

I have looked at the documentation here https://www.jetbrains.com/idea/help/configuring-module-dependencies-and-libraries.html and followed the instructions with no success.

So the dynamic library is being loaded and I have added the directory containing libhyfiModel.jnilib as a dependency but the method can not execute... What am I missing?

EDIT:

I created a new IntelliJ project with just Temp.java and I am able to call the native function with no errors, so I'm thinking there must be a problem with my project configurations. Any IntelliJ experts out there?

1 Answer 1

0

Figured it out. It wasn't a configuration error since the library was being loaded properly. The problem was with Temp.java package membership used in the IntelliJ project.

I had a Temp.h file that was generated using the command javah -jni Temp that contains the C function definitions for my native functions.

I didn't realize that the native function definitions in Temp.h are different when the Java file Temp.java has a specified package.

I found my answer at Another JNI, C++, DLL, UnsatisfiedLinkError <Native Method>

It is shown that the header files' function names change with package membership.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.