A simple use-sharing _java of JNI in Java

Source: Internet
Author: User

Knowing that Jni:java is popular because of its cross-platform nature, and thus makes it very rare with native internal connections, JNI (Java Native Interface) is a way to solve Java local operations. Java invokes the local method through JNI, and the local method is stored in the form of a library file (in the form of a DLL file on a Windows platform and a so file form on a UNIX machine). By invoking the internal method of the local library file, Java can implement close contact with the local machine and invoke the system-level interface methods.

The steps are as follows:

1, write well. Java source files:

Copy Code code as follows:

Package com.home;

public class Testdll {
Declares the library name that is invoked
static {
System.loadlibrary ("Hello");
}

Make a local declaration of the method that will be invoked, native keyword is not rare
public native static int get ();

Public native static void set (int i);

public static void Main (string[] args) {
Testdll test = new Testdll ();
Test.set (5);
System.out.println (Test.get ());
}
}



2, compile it with Javac Testdll.java, will generate Testdll.class.





3, and then use the Javah command to generate the Testdll.h file, this file needs to be called C + + program to generate the required library files.





Note operation: First into the SRC directory, and then execute javah-classpath. -jni Com.home.Testdll





4, for the generated. h header file, C + + needs to do is to implement its various methods concretely. Then compile the connection into a library file. And then copy the library files to the Java program's path, you can use Java to invoke the functions implemented by C + +.





This is just the generated. h header file (com_home_testdll.h)


Copy Code code as follows:

/* Don't EDIT this file-it is machine generated */ 
#include < jni.h>  
/* Header for Class Com_home_testdll */ 

#ifndef _INCLUDED_COM_HOME_TESTDLL&N bsp; 
#define _included_com_home_testdll  
#ifdef __cplusplus  
extern "C" {&NB sp; 
#endif   
/* 
 * class:     com_home_testdll   * method:    get 
 * Signature: () i 
 */ 
Jniexport jint Jnicall java_com_home_testdll_get  
  (jnienv *, Jclass);  

/* 
&nb sp;* class:     com_home_testdll 
 * method:    set 
 * Signature: (I) v 
 */ 
jniexport void Jnicall java_com_home_testdll_set     (JNIEnv *, Jclass, JinT);  

#ifdef __cplusplus  
}  
#endif   
#endif


When
concretely implemented, we only care about the two function prototypes


Copy Code code as follows:

Jniexport jint jnicall java_com_home_testdll_get (jnienv *, jclass);

Jniexport void Jnicall Java_com_home_testdll_set (jnienv *, Jclass, jint);




here Jniexport and jnicall are all JNI keywords that indicate that this function is to be called by JNI. And Jint is a type that uses JNI as an intermediary to communicate the int type of Java with the local int, and we can ignore it as int. The name of the function is Java_ plus the package path of the Java program plus the number of functions. parameter, we only need to care about the parameters that exist in the Java program, as for jnienv* and jclass we generally do not need to touch it.





5, we use Testdll.cpp file to implement these two functions concretely:


Copy Code code as follows:

#include "com_home_testdll.h"

int i = 0;

Jniexport jint jnicall java_com_home_testdll_get (jnienv *, Jclass)

{

return i;

}

Jniexport void Jnicall Java_com_home_testdll_set (jnienv *, Jclass, Jint)

{

i = j;

}



6, compile and connect to DLL library file. And the name is hello (to be consistent with calls in Java).




The
action can use the CL command for VS





7, copy Hello.dll to Testdll.class directory, then Java Testdll run it, OK.

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.