Tell the Java VM to Load the DLL
There are two different ways to load a native library into a running Java program:
System.loadLibrary(String) and
System.load(String). The
First,
When using java.library.path=C:\WINNT\system32;.;C:\WINNT\System32;C:\WINNT;c:\applications\asc\pervasive\BIN;c:\cygwin\bin; C:\WINNT\system32;C:\WINNT;C:\WINNT\System32\Wbem;\win32 C:\>echo %PATH% c:\applications\asc\pervasive\BIN;c:\cygwin\bin;C:\WINNT\system32;C:\WINNT;C:\WINNT\System32\Wbem;\win32Note that the current directory is inserted into the PATH ; I believe that this is something that Windows
does by default. I am going to execute the program from the directory in which HelloWorld.dll was created,
so I won't have to mess with java.library.path . One could also use command-line options to alter
java.library.path or simply copy the DLL into one of the Windows directories.
In order to cause the JVM to load the library, we need to alter our Java code: package example.jni; public class HelloWorld { private static native void writeHelloWorldToStdout(); public static void main(String[] args) { System.loadLibrary("HelloWorld"); writeHelloWorldToStdout(); } } Go ahead and re-compile the Java program. The JVM takes care of resolving "HelloWorld" to "HelloWorld.dll" (on UNIX, it would resolve it to "libHelloWorld.so"). Anyway, we're finished and can proceed to the next step.
If we were to use the System.load("c:/path/to/dll/HelloWorld.dll");By the way, that's a good non-JNI tip -- the JVM accepts forward slashes just fine, even for Windows file names. Some of my programs (e.g., web programs which involve forward slashes in URLs) have been vastly simplified knowing that, not to mention the lack of annoying escape characters. (Disclaimer: As far as I know, this behavior isn't part of the Java specification, so it's possible it could be changed or operate differently in other VMs.) |