title: Compiling JNI C++ Native Code with x86_64-w64-mingw32-g++
body: <p>I want to compile and run a simple Hello World program that declares and calls a native print method (defined in C++) from Java.</p>
  
  <p>HelloCPP.java</p>
  
  <pre><code>class HelloCPP{
      private native void print();
      public static void main(String [] args){
          new HelloCPP().print();
      }
      static{
          System.loadLibrary("HelloCPP");
      } 
  }
  </code></pre>
  
  <p>HelloCPP.cpp</p>
  
  <pre><code>#include &lt;jni.h&gt;
  #include&lt;iostream&gt;
  #include "HelloCPP.h" 
  using namespace std;
  
  extern "C" 
  JNIEXPORT void JNICALL Java_HelloCPP_print(JNIEnv *env, jobject obj){
      cout &lt;&lt; "Hello World from C++!" &lt;&lt; endl;
      return; 
  }
  </code></pre>
  
  <p><strong>In the command prompt I run the following:</strong></p>
  
  <ul>
  <li>javac HelloCPP.java</li>
  <li>javah -jni HelloCPP</li>
  <li>86_64-w64-mingw32-g++ -c -I"C:\Java\jdk1.8.0_171\include" -I"C:\Java\jdk1.8.0_171\include\win32" HelloCPP.cpp</li>
  <li>x86_64-w64-mingw32-g++ -shared -o HelloCPP.dll HelloCPP.o</li>
  <li>java -Djava.library.path=. HelloCPP</li>
  </ul>
  
  <p><strong>Then the infamous DLL linking/loading error</strong></p>
  
  <blockquote>
    <p>Exception in thread "main" java.lang.UnsatisfiedLinkError:&lt;"MyProjectDirectory">\HelloCPP.dll: Can't find dependent libraries at java.lang.ClassLoader$NativeLibrary.load(Native Method) at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1941) at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1857) at java.lang.Runtime.loadLibrary0(Runtime.java:870) at java.lang.System.loadLibrary(System.java:1122) at HelloCPP.(HelloCPP.java:8)</p>
  </blockquote>
  
  <p><strong>Attempted Solutions</strong></p>
  
  <ul>
  <li>java -Djava.library.path=. HelloCPP is supposed to check the current project directory which is where the dll is built and should link fine</li>
  <li>Tried copying the dll into a another folder and adding that folder directory to the path system environment variable</li>
  <li>Used the Dependency Walker and noticed HelloCPP.dll is looking for libstdc++-6.dll and other dlls</li>
  <li>Tried copying these drivers into the same directory and then it causes a chain reaction looking for additional dlls</li>
  <li>Tried adding commands like -static-libstdc++</li>
  </ul>
  
  <p><strong>Additional Notes</strong></p>
  
  <ul>
  <li>I can run this exact program with the native method in C and using x86_64-w64-mingw32-gcc, the DLL is found, linked, loaded and program runs fine</li>
  <li>x86_64-w64-mingw32-g++ was downloaded through Cygwin</li>
  <li>Switched to x86_64-w64-mingw32-g++ because I was having the same issue as <a href="https://stackoverflow.com/questions/44085213/cygwin-compilation-error-problematic-frame-c-cygwin1-dll0xd6d47-using-jni">Cygwin compilation error Problematic frame: # C [cygwin1.dll+0xd6d47] using JNI</a> when using g++</li>
  <li>Currently using Sublime text editor to write my Java and C++ files, then compiling through command prompt</li>
  <li>If anyone can recommend me a good IDE to use for dev with JNI that would be great</li>
  <li>First time posting hopefully did not miss anything important, have spent much time going through already asked questions</li>
  </ul>