失效链接处理 |
Java Native Interface (JNI) - Java Programming Tutorial PDF 下载
本站整理下载:
相关截图:
![]()
主要内容:
The static initializer invokes System.loadLibrary() to load the native library "hello" (which contains a native method called sayHello()) during
the class loading. It will be mapped to "hello.dll" in Windows; or "libhello.so" in Unixes/Mac OS X. This library shall be included in Java's library
path (kept in Java system variable java.library.path). You could include the library into Java's library path via VM argument -
Djava.library.path=/path/to/lib. The program will throw a UnsatisfiedLinkError if the library cannot be found in runtime.
Next, we declare the method sayHello() as a native instance method, via keyword native which denotes that this method is implemented in
another language. A native method does not contain a body. The sayHello() shall be found in the native library loaded.
The main() method allocates an instance of HelloJNI and invoke the native method sayHello().
Step 2: Compile the Java Program HelloJNI.java & Generate the C/C++ Header File HelloJNI.h
Starting from JDK 8, you should use "javac -h" to compile the Java program AND generate C/C++ header file called HelloJNI.h as follows:
> javac -h . HelloJNI.java
The "-h dir" option generates C/C++ header and places it in the directory specified (in the above example, '.' for the current directory).
Before JDK 8, you need to compile the Java program using javac and generate C/C++ header using a dedicated javah utility, as follows. The javah
utility is no longer available in JDK 10.
> javac HelloJNI.java
> javah HelloJNI
Inspect the header file HelloJNI.h:
|