Local Communication between Java and C/C ++ heterogeneous platforms allows Java to deal with hardware

Source: Internet
Author: User

Java itself is powerful enough, but in some cases, Java is also limited. The biggest limitation is that it cannot directly operate on hardware, Because Java runs on JVM, not directly running on the CPU. Another reason is that Java is less efficient than C/C ++, but C/C ++ is more efficient and can directly operate hardware, however, the disadvantage is that the development efficiency is too low. Let's combine Java with C/C ++! Leverage their respective advantages to maximize their potential.

Here we use JNI technology, Java Native Interface (Java Local interface)

It is also very simple to say, that is, to define business logic interfaces using Java, and then to implement these interfaces using C/C ++, and these implementations are transparent to Java calls.

 

 

First create a Java project jnidemo

As follows:

Defines a userinfo class and a userservice business logic object

Userinfo class, with only one ID and name member variable

 

Userservice business logic

Static {}.

It is worth noting that two methods, countuser and finduserbyid, are defined here, but they are not implemented and are marked with native.

This shows that these two methods are local hosting methods. Generally speaking, they are implemented by C/C ++, rather than Java.

 

 

 

Well, the above is the Java project. Let's take a look at the C/C ++ project.

 

The IDE is visual stdio 2008.

Complicated here

1. Step 1: export the C/C ++ header file hosted by the Java-hosted userservice business logic class

Procedure: Start-> Run-> cmd

Go to the root directory or bin directory of the jnidemo project (the web project does not have the bin directory ),

Then run javah-JNI com. Sean. JNI. Service. userservice (absolute path of the class)

Then, the generated C/C ++ header file is displayed in the bin directory.

Create a C/C ++ project. Use vs to create an mfc dll project and copy the generated header file com_sean_jni_service_userservice.h to the project. H (in the % java_home %/include directory) and jni_md.h (in the % java_home %/include/Win32 Directory) the two header files are copied to the project together, and then the code is written to create userservice. CPP file (or. c file) the code is as follows. We can see that the two methods are implemented here. For the sake of simplicity, countuser and finduserbyid are directly returned to countuser, while in the finduserbyid method, create a userinfo object using the ID passed in by Java, id = ID passed in by Java, name = "Sean", and return it to Java. If you are familiar with Java reflection, you can see that the Code implemented in C ++ is a bit reflective? Well, that's right. Well, the C/C ++ project is complete. The next step is to connect Java and C/C ++ and compile the C/C ++ project, a dll file, jnidemo, is generated in the DEBUG directory of the project. DLL (the specific file name is the project name)

 

Okay. You need this DLL file, dynamic link library (dynamic link library)

 

 

 

Copy this DLL to a Java project and create a new DLL code folder (optional)

Then, in the Java class userservice, add a code static

{

System. loadlibrary ("DLL/jnidemo ");

} That's right. It means to import this library to the Java project so far, and the entire project is finished. Java calls C/C ++, to process the business logic, write a test function.

 

The code is simple. First, call the countuser method to calculate the total number of persons? In the C/C ++ project, 100 is returned. Therefore, 100 is printed and finduserbyid (10000) is called. The parameter is 10000. Therefore, the returned user object, id = 10000, name = Sean in order to test the efficiency of Java Calling C/C ++, we also counted the call time. The code above should run in total: 100the parameter is: 10000 user ID: 10000 User name: Sean spent a total of: the running result of XXX milliseconds is completely correct, and it took only 4 milliseconds (the actual result must be less than 4 milliseconds ), although the speed is certainly not comparable to running C/C ++ directly, this speed is acceptable compared to Java. Through this case, we can also think a little farther. Although Java cannot directly operate on hardware, can it allow C/C ++ to operate on hardware and then use Java to directly call it? The answer is yes, which means that Java and C/C ++ can be developed in collaboration with each other.

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.