4

I would like to use an existing static C++ library in my Java program. I read that it should be possible. And the steps would be (nearly) the same as for linking dynamic libraries. Unfortunately if I try to load the library with:

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

I get an exception:

Exception in thread "main" java.lang.UnsatisfiedLinkError: no mylibrary in java.library.path

I tried setting the java.library.path before the System.loadLibrary call manually, but it does not help:

System.setProperty("java.library.path", "/workspace/LibraryTest/lib/");

Setting LD_LIBRARY_PATH in run configurations for the project does also not fix the problem. I get the same error. For shared libraries it works fine though. Currently I'm using Oracle Java8 in Eclipse Mars on Debian Jessie. Does the library need to hava a JNI_OnLoad_mylibrary method? Anyone experienced similar problems?

EDIT

tried /usr/lib directory and added JNI_OnLoad_mylibrary. No success.

Community
  • 1
  • 1
little_planet
  • 1,005
  • 1
  • 16
  • 35
  • This has been answered before, rather well, here: http://stackoverflow.com/questions/24493337/linking-static-library-with-jni – Chris Kitching Mar 03 '16 at 19:07
  • That was the post i already linked in my question. I know it has to be possible, but it is not working. Maybe i missed something? – little_planet Mar 03 '16 at 19:13

1 Answers1

0

Perhaps your code isn't relocatable? That seems like it might upset the JVM. Recompile with -fPIC and see what happens.

From the answer to the other question, you need JNI_OnLoad_name-of-your-library to be defined in the binary for this to work. Did C++ name mangling ruin your day? You'll probably need to extern "C" that function for it to be findable to the JVM.

Chris Kitching
  • 2,559
  • 23
  • 37
  • 1
    I updated the library and added the extern keyword for the JNI_OnLoad_mylibrary (an UnLoad) method. Also compiled the library with -fPIC. No errors during compile time. The library runs fine if I call it from C++. But Eclipse is still telling me "no mylibrary in java.library.path". Just to be sure, it is sufficient to name the method JNI_OnLoad_mylibrary if the static library file name is libmylibrary.a? – little_planet Mar 03 '16 at 19:31
  • 1
    I can answer the question in my last comment: yes, it is sufficient to name the method JNI_OnLoad_mylibrary. At least that is, what the JNI documentations says. However the main problem, that the static library is not found remains. Any ideas or hints are highly appreciated. – little_planet Mar 04 '16 at 07:48
  • You could compile the static library into a shared library and use that: http://stackoverflow.com/questions/655163/convert-a-static-library-to-a-shared-library Other than that, I'm prettymuch out of ideas. What you're doing _should work_. Maybe you compiled the library for the wrong arch? Wrong bitness? Used processor extensions you don't have? – Chris Kitching Mar 04 '16 at 17:04
  • 1
    Currently I'm making a shared library from the static one. Sadly I don't have the time to check all the possible problems... Guess I will have look into this in more detail in the future. If I find a solution I will post it here. – little_planet Mar 04 '16 at 17:13
  • Sorry I can't be more help. The JNI is very fussy about a whole bunch of different things, and it doesn't often give good errors when things go wrong. You often end up stuck with "Check _all_ the things!" – Chris Kitching Mar 04 '16 at 17:14