Yavuz
Yavuz

Reputation: 1393

Can't use jni generated dll from eclipse

I have generated a dll file with jni using command prompt. I can run the code below with "java helloWorld" command. But I can't do it from eclipse. When I ran the program I get an error, that says:

Exception in thread "main" java.lang.UnsatisfiedLinkError: no native_library in java.library.path at java.lang.ClassLoader.loadLibrary(Unknown Source) at java.lang.Runtime.loadLibrary0(Unknown Source) at java.lang.System.loadLibrary(Unknown Source) at helloWorld.(helloWorld.java:6)

I have used absolute path for native_library as you can see below. What's the problem with it?

helloWorld.java:

public class helloWorld {

    static{     
        System.loadLibrary("native_library");
        System.load("C:/javaworkspace/helloWorld/src/native_library.dll");
        }   

    public static native void writeout(String ss);

    public static void main(String[] args) {    
        String sdf="Hello World";   
        writeout(sdf);              
    }
}

native_library.c:

#include <stdio.h>
#include "helloWorld.h"

JNIEXPORT void JNICALL Java_helloWorld_writeout
  (JNIEnv * env, jclass clazz, jstring str2)
  {
    const char *nativeString = (*env)->GetStringUTFChars(env, str2, 0);

    printf("%s \n",nativeString);

    (*env)->ReleaseStringUTFChars(env, str2, nativeString);
  }

Upvotes: 0

Views: 292

Answers (2)

Szymon Jednac
Szymon Jednac

Reputation: 3007

Hardcoding your DLL path isn't the best idea in my opinion. Use a command line argument instead:

java -Djava.library.path=<path_to_lib_directory>

For Eclipse: open your Build Path (Right click on the project > "Build path" > "Configure Build Path...") and set the "Native library location" attribute.

Upvotes: 2

BackSlash
BackSlash

Reputation: 22243

I had the same issue, i think it is because Ecplipse has it's own library path, that is different from your dll path, try adding

System.setProperty("java.library.path","your_dll_path");

i.e.

System.setProperty("java.library.path","/home/user/helloworldlib");

It worked for me, hope this helps!

Upvotes: 0

Related Questions