The trick is binding our Java class that depends on the library to the actual loading of that library. This way, if Java is unable to find the dependent C library the loading of the dependent Java class will fail. Here's an example:
import java.util.Linker; public class Demo { static { Linker.loadLibrary("demo"); } public native void close(); public native int read(byte b[], int len); . . . }By using the Linker.loadLibrary() method in the dependent Java class's static initializer, class Demo's loading will fail if we are unable to find the "demo" library. It's worth noting that Linker.loadLibrary takes the library name and builds a system dependent dynamically loadable library name out of it. For example, on Solaris it looks for libdemo.so. The Linker uses LD_LIBRARY_PATH to search for the dynamic library.
Implementing Native Methods
Generated with CERN WebMaker