Take off the cloak of JNI (-)

Source: Internet
Author: User
Tags function prototype

Take off the cloak of JNI
    
We all know that Java is a Cross-platform language, and is a completely OO language, in its own language features are also used very easy to use, so most of the project is a priority to choose a development platform, especially in dealing with a more powerful system on the development of the rapid convenience, then it has some shortcomings, Very weak when dealing with local methods, because we cannot directly manipulate memory directly, memory management is handled through the JVM. So it's not a good choice for low-level development, so we'll do it with C + + when it comes to low-level development, so if you're in a business system, Some subsystems need to invoke the underlying application, or the underlying controls provided by the third party. So how do we deal with it (for example, the medical subsystem in social security needs to read the user's card information)? JNI is a good choice for us.

a simple JNI Call
First of all, if you want to define a local method in Java, you need to add a keyword native to this method, for example, define a method named Dosoming, which is shown in Java as follows
    Public native void dosoming ();
Of course the local method is defined to implement it, then we need to create. h and. cpp two files in C + +, and we have a tool in the JDK to help us build. h files. In JDK Javah.exe in the bin directory. Note that when using it we need to switch to the classpath and then enter the command, which requires the full package path of the class file to be entered in the format as follows
Javah Test.jni.JNITest
This will generate a. h file named after the package path in our directory, in my case test_jni_jnitest.h, which will include all methods defined as native in Java in the. h file, the function prototype is as follows
jniexport void Jnicall java_test_jni_jnitest_dosoming
(JNIENV *, jobject);
We use vc++6.0 to create a dynamic library Win32 the console project, the Test_jni_jnitest.h to join in, and then according to the function prototype, we create a new CPP file, which simply write a simple console output, and the head file included.
Jniexport void Jnicall java_test_jni_jnitest_dosoming (jnienv * env, Jobject obj)
{
cout<< "http://blog.111cn.net/zxm0412" <<endl;
}
Compile the project, will be prompted to find jni.h and jni_md.h This is because there is no. h file include to the project, under the Tool->options->directories can be set. We generate a DLL file after the compilation passes, You need to put this DLL file under the system path so that our Java program can find it.
The Java caller's code is as follows
Public Static void Main (string[] args)
    {
jnitest test = <>
<

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.