How to use JNI to invoke functions written by C + + in Java

Source: Internet
Author: User

Java, which is popular with its cross-platform features, and because of its cross-platform purpose, makes it less powerful and its internal connections to local machines, constraining its functionality. One way to solve Java's local operations is JNI.

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.

Simple introduction and application are as follows:

One, Java needs to do the work

In a Java program, you first need to declare the name of the library called in the class, as follows:

static {

System.loadlibrary ("Testdll");

}

Here, the extension of the library name can not be written out, whether it is a DLL or so, by the system itself to judge.

You also need to make a local declaration of the method that will be invoked, with the keyword native. And only need to declare, and not need to implement specifically. As follows:

Public native static void set (int i);

public native static int get ();

Then you compile the Java program file, generate class, and then use the Javah command, and JNI will generate the header file for C + +.



For example, program Testdll.java, the content is:

public class Testdll

{

Static

{

System.loadlibrary ("Testdll");

}



public native static int get ();

Public native static void set (int i);



public static void Main (string[] args)

{

Testdll test = new Testdll ();

Test.set (10);

System.out.println (Test.get ());

}

}

Compiling it with Javac Testdll.java generates Testdll.class.

With Javah Testdll, the testdll.h file will be generated in the current directory, which needs to be called by C + + program to generate the required library files.



Ii. work to be done in C/A + +

For a generated. h header file, C + + needs to do is to implement each of its 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 + +.



Take the example. Let's take a look at the contents of the Testdll.h file:

/* Do isn't EDIT this file-it is machine generated * *

#include

/* Header for class Testdll * *



#ifndef _included_testdll

#define _included_testdll

#ifdef __cplusplus

extern "C" {

#endif

/*

* Class:testdll

* Method:get

* Signature: () I

*/

Jniexport Jint Jnicall Java_testdll_get

(JNIENV *, jclass);



/*

* Class:testdll

* Method:set

* Signature: (I) V

*/

jniexport void Jnicall Java_testdll_set

(JNIEnv *, Jclass, jint);



#ifdef __cplusplus

}

#endif

#endif

In the concrete implementation, we only care about two function prototypes

Jniexport jint jnicall java_testdll_get (jnienv *, jclass);

And

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

Here Jniexport and jnicall are all JNI keywords that indicate that this function is 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.

OK, let's use the Testdll.cpp file to implement these two functions concretely:

#include "testdll.h"

int i = 0;

Jniexport jint jnicall java_testdll_get (jnienv *, Jclass)

{

return i;

}

Jniexport void Jnicall Java_testdll_set (jnienv *, Jclass, Jint J)

{

i = j;

}
Compile and connect to a library file, this example is done under Windows, and the DLL file is generated. and the name needs to be the same as the Java call, and here's the Testdll.dll
For in VC6.0, you can open vc++6.0, create a new->; Engineering->; Win32 Dynamic-link Library. Select an empty project in the wizard.
Add the header file that Javah just generated to the project. then file->; new->;textfile make one yourself
. cpp files. Implement your own native function.

For Visual Stdio 2005, you can create a new WIN32 empty project, and then select the DLL and empty project in the next step. Then add the testdll.h and testdll.cpp above and compile to generate Testdll.dll files.

In general, the VC compiler will not find the jni.h and Jni_md.h header files, these two header files can be found under the JDK directory include. vc6.0 may not be able to find the cl.exe compiler, you can download the Internet and then set the environment variable on it. Usually this compiler will be found in the VC directory under the Vc/bin directory.
Copy the Testdll.dll to the Testdll.class directory, and the Java Testdll run it, and you can see the results.

Related Article

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.