3

I was learning JNI which uses libraries, and the tuturial is https://www.baeldung.com/jni. The java source file is

package zxy;

public class JNIDemo {
    public native void testHello();

    public static void main(String[] args){
        System.loadLibrary("native");
        JNIDemo jniDemo = new JNIDemo();
        jniDemo.testHello();
        System.out.println("zxy");
    }
}

and the project directory is:

/src/main/java/zxy
       |->JNIDemo.java
/libs
  |-> libnative.so
build.gradle
gradlew
settings.gradle

the build.gradle file is

plugins {
    id 'application'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

application {
    mainClass = 'zxy.JNIDemo'
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    implementation files('libs/libnative.so')
}

and the settings.gradle is

rootProject.name = 'jni'

When I run ./gradlew installDist, it works fine, and gives my the following dir structure

build/
|-> /install/jni
        |-> /bin
               |-> jni
               |-> jni.bat
        |-> /lib
               |-> jni-1.0-SNAPSHOT.jar
               |-> libnative.so

But when I run build/install/jni/bin/jni, I gives me

Exception in thread "main" java.lang.UnsatisfiedLinkError: no native in java.library.path
        at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1860)
        at java.lang.Runtime.loadLibrary0(Runtime.java:871)
        at java.lang.System.loadLibrary(System.java:1124)
        at zxy.JNIDemo.main(JNIDemo.java:8)

FAILURE: Build failed with an exception.

I dont understand why, how can I include java.library.path in command gradlew installDist

K. Zhu
  • 358
  • 1
  • 3
  • 12
  • Are you using ubuntu or windows ? – George Nov 15 '21 at 12:13
  • @George ubuntu :-) – K. Zhu Nov 15 '21 at 15:24
  • and u have jni.bat ? – George Nov 15 '21 at 16:19
  • @George It's autogenerated by gradlew, I don't know why. I never use it, I use jni instead. – K. Zhu Nov 15 '21 at 16:29
  • alright , as much as i know , you need to export your .so file to LD_LIBRARY_PATH environment variable , when i tried this with ubuntu the last time it worked like charm . this [answer](https://stackoverflow.com/questions/13428910/how-to-set-the-environmental-variable-ld-library-path-in-linux) can be helpful in your case . – George Nov 15 '21 at 17:06

0 Answers0