0

I am trying to call a go function from scala code using JNI . The function in question is as follows

//export Java_jniroute_TestJNI_printDisplay
func Java_jniroute_TestJNI_printDisplay(env *C.JNIEnv, obj C.jobject) {
size := C.jx_GetIntArrayElements(env, val1, (*C.jboolean)(nil))
fmt.Println("hello from go")

To enable go call jx_GetIntArrayElements i already created a wrapper C lib with code

#include "jni.h"
int* wx_GetIntArrayElements(JNIEnv *env, jintArray array, jboolean *isCopy)
{
 return (*env)->GetIntArrayElements(env, array, isCopy);
}

which was exported to GO using

/*
#cgo LDFLAGS: -L${SRCDIR} -lwc

#include "wc.h"
*/

The compilation went fine and go lib libnnturboroute was created but once i try to load the lib from within Scala using System.loadLibrary(library) I get the following exception

An exception or error caused a run to abort: /libnnturboroute.dylib: dlopen(/libnnturboroute.dylib, 1): Library not loaded: libwc.dylib. It appears that from jvm is not able to load libwc.dylib from within the JNI exposed method. I understand that dynamic dependency libraries need to be called from within JNI code . However i can't load library from with golang code since C.dlopen would itself be needed to be imported via C wrapper library .

ankur jha
  • 11
  • 1
  • Does your problem go away if you set `DYLD_LIBRARY_PATH=$SRCDIR` ? – Botje Apr 11 '20 at 21:39
  • @Botjie Yes , it did . Thanks a lot . – ankur jha Apr 12 '20 at 05:04
  • Then this is a more structural solution: [How to set the runtime path (-rpath) of an executable with gcc under Mac OSX?](https://stackoverflow.com/questions/4513799/how-to-set-the-runtime-path-rpath-of-an-executable-with-gcc-under-mac-osx) – Botje Apr 14 '20 at 06:34
  • @Botjie Correct. Initially I thought that despite the java.library.path being set, the library is not visible which I though must be configuration issue. Hence the question. However, the mistake appears quire obvious since any jvm param won't be available to native libraries for discovering there own dependencies. – ankur jha Apr 15 '20 at 15:21

0 Answers0