Java JNI Quick Start tutorial (recommended) _java

Source: Internet
Author: User
Tags garbage collection

1. JNI Introduction

JNI is the English abbreviation for the Java Native interface, meaning the Java local interface.

Problem Source: Because Java writing the bottom of the application is more difficult to implement, in some real-time requirements very high part of Java is more difficult to do (real-time requirements of the place is not yet involved, real-time such topics to be studied).

Workaround: Java can use JNI to invoke existing local libraries (C + + to develop any and system-related programs and class libraries), greatly flexible Java development.

2. JNI Quick Learning Tutorial

2.1 Questions:

Use JNI to write a piece of code that implements the String_java_test_helloworld (jnienv *env, Jclass cls, jstring j_str) function, which is implemented in front of the string J_str ("World") plus Hello, and returns.

2.2 Problem Solving Process:

I. Preparation of the Test.java category:

public class test{
  //native interface public
  native string HelloWorld (string text);
  
  public static void Main (string[] args) {
    //Load Dynamic Library
    system.loadlibrary ("Test2");
    Test ts = new test ();
    String Text = Ts.helloworld ("World");
    System.out.println (text);
  }

Note:

1, Load dynamic Class Library: System.loadlibrary ("Test2"); "Under Windows Load is Test2.dll,linux load is test2.so"

Ii. Compiling Test.java files

Enter cmd input command > Javac Test.java

Iii. Generating Test.h files

Enter cmd input command > Javah Test

The contents of the Test.h file are as follows:

/* Do isn't EDIT this file-it is machine generated/*
#include <jni.h>/
* Header for class Test/*

#ifnd EF _included_test
#define _included_test
#ifdef __cplusplus
extern "C" {
#endif
 * * Class:   Test
 * method:  HelloWorld
 * Signature: (ljava/lang/string;) ljava/lang/string;
 * *
jniexport jstring jnicall java_test_helloworld
 (jnienv *, Jobject, jstring);

#ifdef __cplusplus
}
#endif
#endif

Note:

1, function declaration, fixed format: jniexport; return type: Jstring;jni Call: Jnicall;java_ Full class Name _ Method name: Java_test_helloworld;

2, function parameters: Call jni.h encapsulated function pointer: Jnienv;java class itself: Jobject,java file incoming parameters: Jstring.

Iv. writing the C language file test2.c implement the function of the test class to invoke the dynamic link library:

#include "Test.h"
#include <string.h>

jniexport jstring jnicall java_test_helloworld
 (jnienv *env, Jobject obj, jstring string) {
  const char* str = (*env)->getstringutfchars (env,string,0);
  Char cap[128];
  Cap[0] = ' h ';
  CAP[1] = ' e ';
  CAP[2] = ' l ';
  CAP[3] = ' l ';
  Cap[4] = ' o ';
   
  strcat (CAP,STR);
   
  (*env)->releasestringutfchars (env,string,0);
  Return (*env)->newstringutf (ENV,CAP);
 }

Note:

1, because Java itself uses double-byte characters, C language itself are single-byte characters, so need to use (*env)->getstringutfchars () to convert between Java and C string;

2, Getstringutfchars () and Newstringutf (), the first is the conversion from UTF8 to C encoding format, the second is based on the string of C to return a UTF8 string;

3, Releasestringutfchars () is used to release objects, there are virtual machines in Java garbage collection, but in the C language these objects must be manually recycled, otherwise it may cause memory leaks.

V. Compiling and running

Compile:

Enter cmd input command > gcc-i "D:\Program files\java\jdk1.8.0_45\include"-i "D:\Program files\java\jdk1.8.0_45\include\win32" --share Test2.c-o Test2.dll

Run:

Enter cmd input command > Java Test

The results of the operation are as follows:

HelloWorld

3. Summary:

Step one: Write a Java Class (Test.java) with the native method, and use the Javac tool to compile the Java class (Generate Test.class);

Step two: Use Javah to generate the header file (Test.h) corresponding to the native method;

The third step is to implement the corresponding header file (test2.c) and compile it as a dynamic-link library (test2.so) using C + +.

This article runs the environment: Windows 64-bit operating system, JDK 1.8 version, MINGW64 (GCC).

The above Java JNI QuickStart Tutorial (recommended) is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.

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.