The full name of JNI is JavaNativeInterface. Through JNI technology, Java and other programming languages can call each other. Here we use the mutual calls between Java and C. Java provides a local interface, and C implements this local interface. The general operation steps for Jni program development are as follows: Compile the java call class and use javah to generate the header file c/c ++ native function to call other required function functions in c/c ++, implement native functions (in principle any resource can be called)
The full name of JNI is a Java Native Interface. Through JNI technology, Java and other programming languages can call each other. Here we use the mutual calls between Java and C. Java provides a local interface, and C implements this local interface.
The general steps for Jni program development are as follows:
- Compile a java call class
- Use javah to generate header files for c/c ++ native functions
- Call other required functions in c/c ++ to implement native functions (in principle, any resource can be called)
- Add all native libraries and resources dependent on the project to java. library. path of the java project.
- Generate a java program
- Publish java applications and so libraries.
Let's just say that the domestic materials are really poor. I used wikipedia's JNI entry and my own practice to write this blog ..
P.S and later, we must work harder to learn English ~~
Http://en.wikipedia.org/wiki/Java_Native_Interface
Below is a simple example of HelloWorld:
Create the following files in any folder:
HelloWorld
Make. sh
- #! /Bin/sh
- JAVA_HOME = $ (readlink-f/usr/bin/javac | sed "s: bin/javac ::")
- Export LD_LIBRARY_PATH = $ LD_LIBRARY_PATH :.
- Javac HelloWorld. java
- Javah HelloWorld
- Gcc-I $ {JAVA_HOME}/include/linux-fPIC-shared-o libHelloWorld. so HelloWorld. c
- Java HelloWorld
Make. sh is a script
Set your own JAVA_HOME
Pay special attention to gcc !!
HelloWorld. java
- ClassHelloWorld
- {
- Private Native VoidPrint ();
- Public Static VoidMain (String [] args)
- {
- NewHelloWorld (). print ();
- }
- Static{
- System. loadLibrary ("HelloWorld ");
- }
- }
HelloWorld. h
- /* Do not edit this file-it is machine generated */
- # Include
- /* Header for class HelloWorld */
- # Ifndef _ Included_HelloWorld
- # Define _ Included_HelloWorld
- # Ifdef _ cplusplus
- Extern"C "{
- # Endif
- /*
- * Class: HelloWorld
- * Method: print
- * Signature: () V
- */
- JNIEXPORTVoidJNICALL Java_HelloWorld_print
- (JNIEnv *, jobject );
- # Ifdef _ cplusplus
- }
- # Endif
- # Endif
HelloWorld. c
- # Include "jni. h"
- # Include
- # Include "HelloWorld. h"
- JNIEXPORTVoidJNICALL
- Java_HelloWorld_print (JNIEnv * env, jobject obj)
- {
- Printf ("Hello World! \ N ");
- Return;
- }
In terminal, enter:
Chmod + x make. sh
./Make. sh
Chmod http://en.wikipedia.org/wiki/Chmod