title: Another JNI, C++, DLL, UnsatisfiedLinkError <Native Method>
body: <p>I've been looking for 2 days now and no solution could help me, so here we go again:</p>
  
  <p>How to fix the UnsatisfiedLinkError... in JNI?</p>
  
  <p>So here's my java code:</p>
  
  <pre><code>package org.lingenio.util;
  
  import java.util.*;
  
  public class PTAPIWrapperForOmegaT {
  
      private native String translateWithPTAPI(String sentence);
  
      private native void test();
  
      public PTAPIWrapperForOmegaT(String sentence) throws Exception{
          System.out.println(sentence);
          test();     
      }
  
      static {
          System.load("C:/Users/michael/Desktop/OmegaT/OmegaT2.3_src/native/PTAPIWrapperForOmegaT.dll");
      }
  }
  </code></pre>
  
  <p>And here's my C++ Code:</p>
  
  <pre><code>    #include &lt;iostream&gt;
      #include &lt;windows.h&gt;
      #include &lt;jni.h&gt;
      #include "PTAPIWrapperForOmegaT.h"
  
      using namespace std;
  
      JNIEXPORT jstring JNICALL Java_PTAPIWrapperForOmegaT_translateWithPTAPI(JNIEnv *env, jobject obj, jstring sentence)
      {
  /* stuff */
      }
  
      JNIEXPORT void JNICALL Java_PTAPIWrapperForOmegaT_test(JNIEnv *, jobject)
      {
          cout &lt;&lt; "This comes from PTAPIWrapperForOmegaT.cpp test();" &lt;&lt; endl;
      }
  
  
      int main(){
          return 0;
      }
  </code></pre>
  
  <p>And the header file:</p>
  
  <pre><code>/* DO NOT EDIT THIS FILE - it is machine generated */
  #include &lt;jni.h&gt;
  /* Header for class PTAPIWrapperForOmegaT */
  
  #ifndef _Included_PTAPIWrapperForOmegaT
  #define _Included_PTAPIWrapperForOmegaT
  #ifdef __cplusplus
  extern "C" {
  #endif
  /*
   * Class:     PTAPIWrapperForOmegaT
   * Method:    translateWithPTAPI
   * Signature: (Ljava/lang/String;)Ljava/lang/String;
   */
  JNIEXPORT jstring JNICALL Java_PTAPIWrapperForOmegaT_translateWithPTAPI
    (JNIEnv *, jobject, jstring);
  
  /*
   * Class:     PTAPIWrapperForOmegaT
   * Method:    test
   * Signature: ()V
   */
  JNIEXPORT void JNICALL Java_PTAPIWrapperForOmegaT_test
    (JNIEnv *, jobject);
  
  #ifdef __cplusplus
  }
  #endif
  #endif
  </code></pre>
  
  <p>and how I build it:</p>
  
  <pre><code>call g++ -Wl,--add-stdcall-alias -c -DBUILDING_EXAMPLE_DLL -I G:/Software/Java/jdk1.7.0_01/include -I G:/Software/Java/jdk1.7.0_01/include/win32 PTAPIWrapperForOmegaT.cpp
  call g++ -shared -Wl,-kill-at -o PTAPIWrapperForOmegaT.dll -I G:/Software/Java/jdk1.7.0_01/include -I G:/Software/Java/jdk1.7.0_01/include/win32 PTAPIWrapperForOmegaT.cpp
  </code></pre>
  
  <p>and finally, the error:</p>
  
  <pre><code>10211: Error: Uncatched exception in thread [Thread-14] 
  10211: Error: java.lang.UnsatisfiedLinkError: org.lingenio.util.PTAPIWrapperForOmegaT.test()V 
  10211: Error:   at org.lingenio.util.PTAPIWrapperForOmegaT.test(Native Method) 
  10211: Error:   at org.lingenio.util.PTAPIWrapperForOmegaT.&lt;init&gt;(PTAPIWrapperForOmegaT.java:13) 
  10211: Error:   at org.omegat.core.machinetranslators.LingenioTranslate.translate(LingenioTranslate.java:32) 
  10211: Error:   at org.omegat.core.machinetranslators.BaseTranslate.getTranslation(BaseTranslate.java:64) 
  10211: Error:   at org.omegat.gui.exttrans.MachineTranslateTextArea$FindThread.search(MachineTranslateTextArea.java:122) 
  10211: Error:   at org.omegat.gui.exttrans.MachineTranslateTextArea$FindThread.search(MachineTranslateTextArea.java:102) 
  10211: Error:   at org.omegat.gui.common.EntryInfoSearchThread.run(EntryInfoSearchThread.java:85) 
  </code></pre>
  
  <p>I don't know exactly about these two lines of g++ here, I think the second one would be sufficient, but some tutorial must have offered the other line as well and I kept it.</p>
  
  <p>I'm on Windows 7, using MingW and the latest Java (1.7xxx I believe).</p>
  
  <p>Any help is appreciated, I suspect the error lies in the compilation, but I just don't know how to go on from here.</p>
  
  <p><strong>EDIT</strong>:</p>
  
  <p>Looking into the dll with DependencyWalker I can see the functions are named like I named them in the .cpp file. Of course I am calling them from the Java Wrapper with their respective names, i.e. test(). Could that be a problem? Can someone who used JNI often in the past tell me whether this is the correct way?</p>