I am trying to use JNI and getting java.lang.UnsatisfiedLinkError. Unlike the other millions of questions asked about this, I have lib in my path and even saw the exception change when I delete it. I am sure something is wrong with the DLL I created, but I am not sure what.
Here is my java class code:
package com; public class Tune { static { System.loadLibrary("lala"); } public static void main(String[] args) { Tune j = new Tune(); System.out.println("2+6="+j.add(2, 6)); } native public int add(int x,int y); }
Here is a shortened part of my javah created header file:
JNIEXPORT jint JNICALL Java_com_Tune_add (JNIEnv *, jobject, jint, jint);
Here is my C ++ code:
#include <jni.h> #include <com_Tune.h> JNIEXPORT jint JNICALL Java_com_Tune_add (JNIEnv * env, jobject obj, jint x, jint y) { return x+y; }
Here is the runtime exception that I get from eclipse:
Exception in thread "main" java.lang.UnsatisfiedLinkError: com.Tune.add(II)I at com.Tune.add(Native Method) at com.Tune.main(Tune.java:9)
I read that the above exception means that he found the lala library, but the add method is not yet defined. The only thing I see differently between my project and the tutorial is:
- Mine uses the package instead of the default package (shouldn't it really be?)
- I have a return value.
- I moved my dll after it was created (I don't think this will break it since my path is configured.)
How is this possible?
Additional Information:
OS: Windows 7
JDK: 1.6.0_31 (for x86, 32 bit jvm)
C ++ IDE: Code :: Blocks (dll was compiled automatically using Code :: Blocks IDE)
C ++ Compiler: MinGW32-g ++ (GNU C ++ Compiler)
I have jni.h and com_Tune.h in C: \ _ \ include
I have lala.dll in C: \ _ \ lib
Environment Variables:
PATH: C: \ Program Files (x86) \ NVIDIA Corporation \ PhysX \ Common;% CommonProgramFiles% \ Microsoft Shared \ Windows Live; C: \ Program Files (x86) \ AMD APP \ bin \ x86_64; C: \ Program Files (x86) \ AMD APP \ bin \ x86;% SystemRoot% \ system32;% SystemRoot%;% SystemRoot% \ System32 \ Wbem;% SYSTEMROOT% \ System32 \ WindowsPowerShell \ v1.0 \; C: \ Program Files (x86) \ ATI Technologies \ ATI.ACE \ Core-Static; C: \ Apps;% JAVA_HOME% \ bin; C: \ Program Files \ MySQL \ MySQL Server 5.5 \ bin;% MAVEN_HOME% \ bin;% HADOOP_INSTALL% \ bin; c: \ Program Files (x86) \ Microsoft SQL Server \ 100 \ Tools \ Binn \; c: \ Program Files \ Microsoft SQL Server \ 100 \ Tools \ Binn \; c: \ Program Files \ Microsoft SQL Server \ 100 \ DTS \ Binn \; C: \ MinGW \ bin; C: \ Program Files (x86) \ GnuWin32 \ bin; C: _ \ path; C: \ _ \ lib; C: \ Program Files (x86) \ Microsoft Visual Studio 10.0 \ VC \ Bin; C: \ _ \ enable