title: JNI -- UnsatisfiedLinkError on native method
body: <p>I'm running Windows XP.  Apparently JNI and UnsatisfiedLinkError go hand in hand...  I've noticed that most of the time, the linker error looks like this:</p>
  
  <pre><code>java.lang.UnsatisfiedLinkError: no whatever.dll in java.library.path
  </code></pre>
  
  <p>But that's not my problem; Java can find my DLL.  I'm getting an error that makes me think i named my method wrong:</p>
  
  <pre><code>java.lang.UnsatisfiedLinkError: NativeTest.nativemethod(lJava/lang/String;)Z
  </code></pre>
  
  <p>I've tried looking at a number of similar questions on StackOverflow like <a href="https://stackoverflow.com/questions/8418346/another-jni-c-dll-unsatisfiedlinkerror-native-method">this one</a>, <a href="https://stackoverflow.com/questions/7108612/unsatisfiedlinkerror-jni">this one</a>, <a href="https://stackoverflow.com/questions/7807665/unsatisfiedlinkerror-while-accessing-a-native-method">this one</a>, <a href="https://stackoverflow.com/questions/8166324/jni-unsatisfiedlinkerror">this one</a>, and <a href="https://stackoverflow.com/questions/9905420/unsatisfiedlinkerror-when-using-jni">this one</a>, but none of these methods have worked.  I also found <a href="http://ubuntuforums.org/showthread.php?t=1104419" rel="nofollow noreferrer">this thread</a> on the Ubuntu forums where it looks like the exact same problem i'm having, but the question asker didn't say how they fixed their own problem (which really sucks).  All Google searches on this have given me an error the same as the java.library.path one.</p>
  
  <p>Here is my actual code.</p>
  
  <p><b>NativeTest.java</b></p>
  
  <pre><code>class NativeTest
      {
  
      public static native boolean nativemethod (String arg);
  
      public static void main (String[] args)
          {
          System.out.println(nativemethod("0123456789"));
          System.out.println(nativemethod("012"));
          }
  
      static { System.loadLibrary("NativeTest"); }
  
      }
  </code></pre>
  
  <p><b>NativeTest.h</b></p>
  
  <pre><code>/* DO NOT EDIT THIS FILE - it is machine generated */
  #include &lt;jni.h&gt;
  /* Header for class NativeTest */
  
  #ifndef _Included_NativeTest
  #define _Included_NativeTest
  #ifdef __cplusplus
  extern "C" {
  #endif
  /*
   * Class:     NativeTest
   * Method:    nativemethod
   * Signature: (Ljava/lang/String;)Z
   */
  JNIEXPORT jboolean JNICALL Java_NativeTest_nativemethod
    (JNIEnv *, jclass, jstring);
  
  #ifdef __cplusplus
  }
  #endif
  #endif
  </code></pre>
  
  <p><b>NativeTest.c</b></p>
  
  <pre><code>#include &lt;jni.h&gt;
  #include &lt;windows.h&gt;
  #include "NativeTest.h"
  
  JNIEXPORT jboolean JNICALL Java_NativeTest_nativemethod
      (JNIEnv* Jenv, jclass Jref, jstring Jarg)
      {
      MessageBox(NULL, "text", "title", MB_OK);
      int len = (*Jenv)-&gt;GetStringLength(Jenv, Jarg);
      return (jboolean)(len &gt; 5);
      }
  </code></pre>
  
  <p><b>In cmd.exe:</b>
  (The gcc command is my hodgepodge of various commands i've found on the internet.)</p>
  
  <pre><code>&gt;javac NativeTest.java
  
  &gt;javah -jni NativeTest
  
  &gt;gcc -shared -I&lt;jdk_dir&gt;\include -I&lt;jdk_dir&gt;\include\win32 -oNativeTest.dll NativeTest.c -lgdi32
  
  &gt;java -Djava.library.path=. NativeTest
  Exception on thread "main" java.lang.UnsatisfiedLinkError: NativeTest.nativemethod(Ljava/lang/String;)Z
          at NativeTest.nativemethod(Native Method)
          at NativeTest.main(NativeTest.java:8)
  
  &gt;java NativeTest
  Exception on thread "main" java.lang.UnsatisfiedLinkError: NativeTest.nativemethod(Ljava/lang/String;)Z
          at NativeTest.nativemethod(Native Method)
          at NativeTest.main(NativeTest.java:8)
  </code></pre>