0

I'm writing a C++ plugin for an application. The plugin is a .o file. Call this code/functionality as being "C".

I also wrote a Java library. Think of the code and functionality as being "J".

Next I used JNI's Invocation API to allow C to call J. Now C can create objects, call methods, pass / receive values from Java objects or static methods. Conceptually:

 C
JNI       method call -------->   J
 C        <----------- Result R
return R

Plugin C calls J via JNI. A useful value R is computed in J, returned to C, and eventually returned to the application.

I individually tested codes J, C, and JNI with "stub mains", and each piece works on its own. R is correct. The problem is when the plugin tries to create a VM using calls to JNI:

./myPlugin.so: undefined symbol: JNI_CreateJavaVM

I believe the problem is that my C++ .o file wants to dynamically link to libjvm.so, but either doesn't know how to, or something is preventing ld.so from linking at runtime.

When I ran JNI as its own code with a stub main (that is, I did essentially the same thing, but as an application with a main function rather than a .o file) the same thing happened, but it was clearly a matter of ld.so not being able to find libjvm.so. Defining:

export LD_LIBRARY_PATH=/usr/java/jdk1.8.0_73/jre/lib/amd64/server

allowed ld.so to do its job correctly. However, defining LD_LIBRARY_PATH to include the Java libs isn't working for my JNI code in the .o file.

Are the rules of dynamic linking different for object file libraries? Should I try to statically link libjvm.so into my .o file? How would one even go about doing that?

I'm on completely uncharted territory for me, and feeling a bit lost. Any help would be greatly appreciated!

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
Mathematician
  • 380
  • 2
  • 9
  • Are you sure you did -ljni during linking? – ALGOholic Mar 01 '16 at 07:12
  • Don't try to call Java from non-Java main-program. (JNI is meant for the opposite: calling native code from Java main-program.) – Lorinczy Zsigmond Apr 04 '16 at 06:11
  • Not true. The Invocation API (part of JNI) does just that. I got it working. The issue was that I had two versions of Java installed on that system. One was being used for compiling the C++ code, and the other was being used to invoke the Java program. The versions were clashing. – Mathematician Sep 08 '16 at 16:54

0 Answers0