jni - JavaCPP, UnsatisfiedLinkError when native library is archived in JAR -


i'm trying call haskell code java, using javacpp create necessary jni binding, discussed in this question.

this how i'm using it:

<rootdir>   /javacpp.jar   /build (destination of libraris)   /src   (contains haskell code)   /com/example/hscode.java (java class load , use native lib) 

content of hscode.java:

package com.example;  import org.bytedeco.javacpp.*; import org.bytedeco.javacpp.annotation.*; import java.util.scanner; import java.io.file; import java.io.filenotfoundexception;  @platform(include={"<hsffi.h>","hscode_stub.h"}) public class hscode {     static { loader.load(); }     public static native void hs_init(int[] argc, @cast("char***") @byptrptr pointerpointer argv);     public static native string code_hs(string text);      public static void main(string[] args) throws filenotfoundexception {         string s = new scanner(new file("test.txt")).usedelimiter("\\z").next();         hs_init(null, null);         string s1 = code_hs(s);         system.out.println(s1);     } } 

compilation:

cd <rootdir> ghc --make -isrc -dynamic -shared -fpic src/hscode.hs \      -o build/libhscode.so -lhsrts-ghc7.8.4 -optl-wl,-rpath,. javac -cp javacpp.jar com/example/hscode.java java -jar javacpp.jar -d build \      -dplatform.compiler=ghc -dplatform.includepath="src:com/example" \      -dplatform.compiler.output="-optl-wl,-rpath,. -optc-o3 -wall build/libhscode.so -dynamic -fpic -shared -lstdc++ -lhsrts-ghc7.8.4 -o " com.example.hscode 

following approach, can create libhscode.so , libjnihscode.so using javacpp, runs fine with:

$ java -cp javacpp.jar:. com.example.hscode 

jar

now, following step want package in jar, , able use jar's com.example.hscode larger java project.

javacpp's page mentions:

[...] moreover, @ runtime, loader.load() method automatically loads native libraries java resources, placed in right directory building process. they can archived in jar file, changes nothing. users not need figure out how make system load files.

so thought should work.

however, if make jar hscode.jar out of content of build folder above, jar contains both libjnihscode.so , libhscode.so, , run with:

$ java -cp javacpp.jar:hscode.jar:. com.example.hscode 

then cannot find native code (exception edited anonymization):

exception in thread "main" java.lang.unsatisfiedlinkerror: no jnihscode in java.library.path     @ java.lang.classloader.loadlibrary(classloader.java:1865)     @ java.lang.runtime.loadlibrary0(runtime.java:870)     @ java.lang.system.loadlibrary(system.java:1122)     @ org.bytedeco.javacpp.loader.loadlibrary(loader.java:597)     @ org.bytedeco.javacpp.loader.load(loader.java:438)     @ org.bytedeco.javacpp.loader.load(loader.java:381)     @ com.example.hscode.<clinit>(hscode.java:13) caused by: java.lang.unsatisfiedlinkerror: /compilation-path/linux-x86_64/libjnihscode.so: hscode.so: cannot open shared object file: no such file or directory     @ java.lang.classloader$nativelibrary.load(native method)     @ java.lang.classloader.loadlibrary0(classloader.java:1937)     @ java.lang.classloader.loadlibrary(classloader.java:1822)     @ java.lang.runtime.load0(runtime.java:809)     @ java.lang.system.load(system.java:1086)     @ org.bytedeco.javacpp.loader.loadlibrary(loader.java:580) 

what missing? know whether javacpp can find native code when it's archived in jar?

building native libraries calling javacpp -jar javacpp.jar com.example.hscode outputs them in com/example/linux-x86_64/ automatically , loader loads them there. when building native libraries other means, still need moved com/example/linux-x86_64/, whether inside jar file or outside normal files, if want loader find them.


Comments