简体   繁体   中英

Use static C++ library in Eclipse for Java

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM