1. Writing Java programs
public class HelloWorld {public native void Displayhelloworld ();//All native keywords are decorated with local declarations static { System.loadlibrary ("Hello");//load local library } public static void Main (string[] args) { new HelloWorld (). Displayhelloworld (); }}
2. Compiling
Javac Helloworld.java
3. Generate a header file with the extension h
Javah HelloWorld
4. Write a method with the same method name declared in the local method implementation and the header file generated by the Javah command.
#include "jni.h" #include "HelloWorld.h"//#includeotherheaders Jniexport void Jnicalljava_helloworld_ Displayhelloworld (Jnienv*env,jobject obj) {printf ("helloworld!\n"); return;}
5. Write Pro file
TARGET = Hellotemplate = Liblanguage = c++config-= qtconfig + Warn_off dllincludepath + = $$ (java_home) \include$$ (JAV A_home) \include\win32headers = Helloworld.hsources = helloworldimpl.c
6. Compiling dynamic link library
Qmakenmake
7. Running the program
Java HelloWorld
Can be output from the console.
Java Intrinsic interface Jni (Java Native Interface) HelloWorld