First knowledge of JNI (Ubuntu12.04 + Java7)

Source: Internet
Author: User
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

  1. #! /Bin/sh
  2. JAVA_HOME = $ (readlink-f/usr/bin/javac | sed "s: bin/javac ::")
  3. Export LD_LIBRARY_PATH = $ LD_LIBRARY_PATH :.
  4. Javac HelloWorld. java
  5. Javah HelloWorld
  6. Gcc-I $ {JAVA_HOME}/include/linux-fPIC-shared-o libHelloWorld. so HelloWorld. c
  7. Java HelloWorld

Make. sh is a script

Set your own JAVA_HOME

Pay special attention to gcc !!

HelloWorld. java

  1. ClassHelloWorld
  2. {
  3. Private Native VoidPrint ();
  4. Public Static VoidMain (String [] args)
  5. {
  6. NewHelloWorld (). print ();
  7. }
  8. Static{
  9. System. loadLibrary ("HelloWorld ");
  10. }
  11. }

HelloWorld. h

  1. /* Do not edit this file-it is machine generated */
  2. # Include
  3. /* Header for class HelloWorld */
  4. # Ifndef _ Included_HelloWorld
  5. # Define _ Included_HelloWorld
  6. # Ifdef _ cplusplus
  7. Extern"C "{
  8. # Endif
  9. /*
  10. * Class: HelloWorld
  11. * Method: print
  12. * Signature: () V
  13. */
  14. JNIEXPORTVoidJNICALL Java_HelloWorld_print
  15. (JNIEnv *, jobject );
  16. # Ifdef _ cplusplus
  17. }
  18. # Endif
  19. # Endif

HelloWorld. c

  1. # Include "jni. h"
  2. # Include
  3. # Include "HelloWorld. h"
  4. JNIEXPORTVoidJNICALL
  5. Java_HelloWorld_print (JNIEnv * env, jobject obj)
  6. {
  7. Printf ("Hello World! \ N ");
  8. Return;
  9. }

In terminal, enter:

Chmod + x make. sh

./Make. sh

Chmod http://en.wikipedia.org/wiki/Chmod

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.