Java jni learning and javajni Learning

Source: Internet
Author: User

Java jni learning and javajni Learning

JAVA JNI

 

JNIIs the abbreviation of Java Native Interface. Java Native Interface (JNI) standard has becomeJavaPart of the platform, which allows Java code to interact with code written in other languages. JNI was initially designed for locally compiled languages, especially C and C ++, but it does not prevent you from using other languages, as long as the call conventions are supported.

Purpose

Standard java class libraries may not support the features required by your program. JNI

Maybe you already have a library or program written in other languages, and you want to use it in java programs.

You may need to use the underlying language to implement a small time-sensitive code, such as assembly, and then call these functions in your java program.

 

 

JNISteps:

 

1,Compile a JAVA program

Public class JniTest {

Native int add (int a, int B );

//AllnativeKeyword modifiers are local declarations.

        Static {

           System.loadLibrary(“add”);

           //Load dynamic library(libadd.so)

}

public static void main(String [] args){

      System.out.println(“start java app”);

      JniTest jniTest = new JniTest();

      int ret;

      ret = jniTest.add(4,6);

      System.out.println(“jni test result is: ” + ret);

 

}

}

 

2,Compile a JAVA program

Javac JniTest. java

Javah-jni JniTestJniTest. h is generated as follows:

 

Change JniTest. h to add. c (mv JniTest. h add. c), and set the required file as follows:

 

2. 1JNI function naming rules

Java (Fixed) _ <package name, such as com_android_xxx >_< class name >_< Function Name>

Java_JniTest_add

Fixed class name function name (JAVA is not packaged in this example)

3,Compile a dynamic library

Gcc-fPIC-c add. c-o add. o

During compilation, files such as jni. h cannot be found. Therefore, use the absolute path to specify the header file: Modify the file in add. c as follows:

 

Modify the following in jni. h:

 

 

Gcc-shared add. o-o libadd. so

//Generate libadd. so dynamic library

4,Execute JAVA program

LD_LIBRAY_PATH =. java JniTest

//Add a dynamic library to the program from the current directory

Execution result:

start java app

jni test result is: 10

 

 

Conclusion: During the process, pay attention to the header file, jni. h is provided by java (compiler), so in <JAVA_HOME>/include/; we usually write the linux program header file under/usr/include/(provided by gcc)

OK, Complete !!!!!!!!!!!!

 


I am a beginner in JAVA and have a problem with JNI.

The classpath error is correct as follows:
Classpath = C: \ Program Files \ Java \ jdk1.7.0 \ lib \ dt. jarC: \ Program Files \ Java \ jdk1.7.0 \ lib \ tools. jar;

Java native (JNI) question!

Use System. loadLibrary to add dll to System path at the same time
Although this method is clumsy
But the problem should be solved.

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.