Search code examples
javacgccjava-native-interface

Exception in thread "main" java.lang.UnsatisfiedLinkError 3


Hi guys i have tried all the solutions like java -Djava.library.path=. demo adding the dll path to PATH java -Djava.library.path=c:\JNI\demo.dll demo

But still the above error.

Here is my java code..

class demo
{
    public native void printline();
    public static void main(String[]args)
    {
        new demo().printline();
    }
}

Here is my c code...

#include<stdio.h>
#include<jni.h>

#include "demo.h"

JNIEXPORT void JNICALL Java_demo_printline(JNIEnv *a, jobject b)
{
    printf("Hello wrold!!!");
    return;
}

Steps for compiling and running,

  1. javac demo.java
  2. javah demo
  3. gcc -c -I"c:\jdk1.7.0_55\include" -I"c:\jdk1.7.0_55\include\win32" demo.c
  4. gcc -Wl,--add-stdcall-alias -shared -o demo.dll demo.c
  5. java -Djava.library.path=c:\JNI\demo.dll demo

Am i going wrong somewhere?

can someone please help me out,.


Solution

  • Try Run-Time Loading of the dll file within the java code in a static block like:

    static
    {
    System.loadLibrary("demo");
    }
    

    should give you the output.

    Moreover make sure that dll file generated is x32 or x64 according to the gcc compiler in use.