0

I am trying to use JNI with C++. I'm able to create a Hello-World JNI project following this tutorial.

But, I want to use JSON for modern C++ inside my C++ function. This library contains only a json.hpp file that I include inside my project. Everything compile well, I got my hello.dll file but when I run my Java application, I got this error :

$ java HelloJNI java.lang.UnsatisfiedLinkError: C:\Users\Levio-CIL\Desktop\TEST\hello.dll: Can't find dependent libraries
        at java.lang.ClassLoader$NativeLibrary.load(Native Method)
        at java.lang.ClassLoader.loadLibrary0(Unknown Source)
        at java.lang.ClassLoader.loadLibrary(Unknown Source)
        at java.lang.Runtime.loadLibrary0(Unknown Source)
        at java.lang.System.loadLibrary(Unknown Source)
        at HelloJNI.<clinit>(HelloJNI.java:3) Exception in thread "main"

I did some tests, and it seems that the .dll can't find the function inside json.hpp. When I remove this function, everything works well. The C++ program can also run successfully as a standalone.

HelloJNI.java:

public class HelloJNI {
   static {
      System.loadLibrary("hello"); 
   }

   private native void sayHello();

   public static void main(String[] args) {
      new HelloJNI().sayHello();  // invoke the native method
   }
}

HelloJNI.cpp:

#include <jni.h>
#include "HelloJNI.h"
#include "json.hpp"

using json = nlohmann::json;

// Implementation of native method sayHello() of HelloJNI class
JNIEXPORT void JNICALL Java_HelloJNI_sayHello(JNIEnv *env, jobject thisObj) {

    json specific_data = json::parse("{\"id\": \"aa\",\"value\": 1,\"type\": \"water\"}"); //THE PROBLEMATIC LINE
    printf("Hello world");
    return;
}

Additional Information :

I'm Using Windows 10 with cygwin64.

I compile my c++ file with this command :

x86_64-w64-mingw32-g++ -I"%JAVA_HOME%\include" -I"%JAVA_HOME%\include\win32" -shared -o hello.dll HelloJNI.cpp json.hpp

Every file is inside the same directory.

EDIT : I included missing library provided by Dependency Walker but still got the same error. (And Yes, I run with the .dll inside my workdir)

Gazouu
  • 372
  • 3
  • 16
  • Is the json.hpp a pure include file (which contains only declarations), or does it also contain the implementations of all the class methods? – Rann Lifshitz May 18 '18 at 15:17
  • @RannLifshitz It's a single include file located here https://github.com/nlohmann/json/blob/develop/single_include/nlohmann/json.hpp – Gazouu May 18 '18 at 15:19
  • Possible duplicate of [How to fix an UnsatisfiedLinkError (Can't find dependent libraries) in a JNI project](https://stackoverflow.com/questions/6092200/how-to-fix-an-unsatisfiedlinkerror-cant-find-dependent-libraries-in-a-jni-pro) – Rann Lifshitz May 18 '18 at 15:29
  • I also read this post before posting but no, it's not a duplicate. I have only one DLL, it's not a problem to link it, my problem is to use a .hpp file inside this .dll. – Gazouu May 18 '18 at 15:35
  • 1
    @Gazouu No, you don't have just one DLL. You have one DLL that you compiled, *and that DLL is also dependent on other DLL(s)*. You need to identify those DLL(s) and make sure your JVM can find them, per the other question. – Andrew Henle May 18 '18 at 15:39
  • Then perhaps the issue is to close the json.hpp in a .dll of it's own, then link it to your own generated .dll, while keeping the json.dll in a recognizable path for the JVM to find during runtime. – Rann Lifshitz May 18 '18 at 15:39

1 Answers1

1

Try to remove hpp from your command,

x86_64-w64-mingw32-g++ -I"%JAVA_HOME%\include" -I"%JAVA_HOME%\include\win32" -shared -o hello.dll HelloJNI.cpp

If the error persists, check the DLL dependencies using the Dependency Walker.

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307