Pytorch C++ with Java Native Interface (JNI) linking error

Hi, I am running into a linking problem when running Torch with JNI. I am not sure if this is the right forum as it might not be directly related to torch but maybe someone might be able to help me:

I have a java program that has native functions declared which are implemented in torchJNI.cpp and defined in torchJNI.h. Of course, for this to work I also load torchJNI with System.loadLibrary("torchJNI");
This works when I create tensors in the native functions and print them etc. The problem comes, when I do more complex things (loss.backward() and optim.step()). In particular, functions that depend on the MKL shared library where the .so files are in /opt/intel/…/.
I implemented a main function in torchJNI.cpp which does training. When compiling and running the torchJNI.cpp file this all of this works perfectly fine. However, when I make a shared library out of torchJNI.cpp/torchJNI.h which gets loaded with JNI and4 I try to run the native method that “has the exact same code” as the main function I get the following error:

INTEL MKL ERROR: /opt/intel/mkl/lib/intel64/ undefined symbol: mkl_lapack_dspevd.    
Intel MKL FATAL ERROR: cannot load or

now I though okay I have to do the same thing to load this shared library, so I added the following lines to the java file:


Now, when I try to run the java file I get:

Exception in thread "main" java.lang.UnsatisfiedLinkError: /opt/intel/mkl/lib/intel64/ /opt/intel/mkl/lib/intel64/ undefined symbol: mkl_lapack_dspevd ....

note, that I run Java with -Djava.library.path=:./path/to/torchJNI/:/opt/intel/mkl/lib/intel64
I looked, and both and are present in /opt/intel/mkl/lib/intel64.

To summarize:

  1. Everything seems to work fine (compilation and runnning) as long as I stay completely in C++.

  2. When I invoke a native function from java that only does simple Torch things that do not require mkl everything also works.

  3. When I invoke a native function from java that uses the torch function backward (for example) I get an INTEL MKL ERROR … undefined symbol: mkl_lapack_dspevd

  4. Trying to fix 3. I tried loading or in Java manually then the error messages changes to a java exception.

Can anyone help me out or take a guess what the problem may be?

This is more of a FYI, but I’ve recently started working on the JavaCPP Presets for PyTorch and that takes care of all those linking issues with MKL and what not by bundling everything in JAR files (actually Maven artifacts):