Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The Unsatisfied Link Error can mean many things went wrong. I would use</p> <pre><code>System.loadLibrary("HelloWorld"); </code></pre> <p>Instead of</p> <pre><code>System.load(); </code></pre> <p>As TwentyMiles suggested.</p> <p>Also, when invoking your program you need to (assuming your DLL is on the same directory as your class files:</p> <p>java -Djava.library.path=. HelloWorld</p> <p>Here's a simple demo I made that calls a Win32 API function (MessageBox)</p> <h2>Java class</h2> <pre><code>class CallApi{ private native String showMessageBox(String msg); private native double getRandomDouble(); static{ try{ System.loadLibrary("CallApi"); System.out.println("Loaded CallApi"); }catch(UnsatisfiedLinkError e){ //nothing to do System.out.println("Couldn't load CallApi"); System.out.println(e.getMessage()); } } public static void main(String args[]){ CallApi api = new CallApi(); double randomNumber = api.getRandomDouble(); String retval = api.showMessageBox("Hello from Java!\n"+ "The native random number: "+randomNumber); System.out.println("The native string: "+retval); } } </code></pre> <h2>Generated header file</h2> <pre><code>/* DO NOT EDIT THIS FILE - it is machine generated */ #include &lt;jni.h&gt; /* Header for class CallApi */ #ifndef _Included_CallApi #define _Included_CallApi #ifdef __cplusplus extern "C" { #endif /* * Class: CallApi * Method: showMessageBox * Signature: (Ljava/lang/String;)Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_CallApi_showMessageBox (JNIEnv *, jobject, jstring); /* * Class: CallApi * Method: getRandomDouble * Signature: ()D */ JNIEXPORT jdouble JNICALL Java_CallApi_getRandomDouble (JNIEnv *, jobject); #ifdef __cplusplus } #endif #endif </code></pre> <h2>The C DLL code</h2> <pre><code>#include "CallApi.h" #include &lt;windows.h&gt; #include &lt;stdlib.h&gt; #include &lt;time.h&gt; #pragma comment(lib,"user32.lib") JNIEXPORT jstring JNICALL Java_CallApi_showMessageBox (JNIEnv *env, jobject thisObject, jstring js) { //first convert jstring to const char for use in MessageBox const jbyte* argvv = (*env)-&gt;GetStringUTFChars(env, js, NULL); char* argv =(char *) argvv; //Call MessageBoxA MessageBox(NULL, argv, "Called from Java!", MB_ICONEXCLAMATION | MB_OK); return js; } JNIEXPORT jdouble JNICALL Java_CallApi_getRandomDouble (JNIEnv *env, jobject thisObject) { double num1; srand((unsigned)(time(0))); num1 = ((double)rand()/(double)RAND_MAX); return num1; } </code></pre> <h2>Compile instructions</h2> <p>I compile with the Visual C++ express 2008 cl, removing the -ML flag since it causes an exception when the Java code tries to call the native code:</p> <p>cl /I"c:\Program Files\Java\jdk1.6.0_10\include" /I"c:\Program Files\Java\jdk1.6.0_10\include\win32" -LD CallApi.c -FeCallApi.dll</p> <p>Then, to run the code:</p> <p>java -Djava.library.path=. CallApi</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload