C + + and Java Hybrid programming

Source: Internet
Author: User

Http://www.cnblogs.com/lxshanye/p/3209542.html

Now the programmer, no longer the same as before, master a programming language can be mixed with a model, the reality is that the real project, usually involves a variety of programming languages, to give a few simple examples, a software for rapid development, may be using Delphi or VB as interface Development preferred language, The underlying instruction or core algorithm, which is processed using C + +, involves data analysis processing using scripting languages such as JavaScript or Python for security and rapid development when dealing with data. Therefore, developers should learn or master language mixing programming. C + + and Java are the main two programming languages, but now the entire network of the implementation of the two languages mixed programming information is few, but not complete, and sometimes a variety of problems are now ambiguous, to the learning or use of these two languages friends caused great trouble. My article my book, I hope to use the two languages mixed programming learning friends can be a catalyst.

Implementation principle

Implementation of Java and C + + interaction, using the technology is called JNI (Java Native Interface), C + + programming, as long as the implementation of the JNI generated interface, you can let the Java program calls, and Java program, C + + calls, You need to run a Java virtual machine to invoke the Java implementation method through a JNI query.

environment variable Settings

The Java version used in this article is (build 1.6.0_03-b05) and C + + is version 6.0 of VC + +. and set the following environment variables based on your native Java and C + + installation directory

Note that you do not lack the Java include and Lib three red line marked parts (for Cpp-env.bat in the source bundle file).
Set the environment variables for Java, as shown in



Note This section of the Red Line callout, which is very important to the C + + call Java method when the Java-env.bat (for this batch file in the source package file). After running these two batch files in command mode, you can run the demo program in command mode.

Java calls the C + + method

The source file in the %src%/java-cpp directory of the Winfile.java of this file (Java syntax specifies that the class name and file name must be consistent), defines a winfile class, the contents of this class are as follows



In line 18th of the code, declare a method with the native property Getfilesfromdir, which passes in a character class parameter and returns a character class argument, and System.loadlibrary will load the specified shared link library, and the dynamic library loaded as shown in the parameters is Libwinfile.dll, and on the Windows platform, the suffix. dll is automatically added when executed. In command mode, run the following command:



The first command generates Winfile.class's compiled file, and the second command generates WinFile.h, which contains the definition of the C + + language for native methods in Winfile.java.

The Java language in the language of the definition of the string is jstring, note that the Java language character and program encoding are implemented in UTF-8 encoding, so in Java, Chinese characters in the C + + method if there is no encoding conversion, it will be displayed as garbled. Similarly, in C + + methods, the Chinese characters are returned to Java, if the character encoding is not converted to UTF-8, the Java method display will also be garbled.

The above is %src%/java-cpp/winfile.cpp part of the code, the code implemented two functions, one is to convert UTF-8 to GB2312, and the other to GB2312 to UTF-8, Jni.h also provides a conversion function between the jstring and char* types in this header file.

1. <b>GetStringUTFChars</b> <b>NewStringUTF</b>

Run the following compile command:

1. <b>cl -GX -LD WinFile.cpp -FelibWinFile.dll</b>

Generate LibWinFile.dll This dynamic library (note that the generated name is the same as the name of the parameter in the function System.loadlibrary), run the Java class.
The output is as follows



C + + calls Java class methods

This shows the method returned by a string as a parameter, and other types of method calls are similar.
To create a statically declared Java method



This method will accept an incoming character parameter for C + + and return the Java character class to let the C + + function output the content. Code in %src%/cpp-java/winfile.java
After compiling the file generates a Java bytecode file, it must be shipped on the JVM, C + + to hold these Java bytecode, you must run the JVM, the code that runs the JVM is in file %src%/cpp-java/winfile.cpp , as shown in

By JNI_CREATEJAVAJVM this function, C + + runs the JVM, noting that Generated WinFile.exe This file prompts for Jvm.dll, but do not copy Jvm.dll from the JRE directory to the WinFile.exe directory, because the JVM is functioning properly and must rely on the Java library of the JRE and other dynamic libraries, although the J is not visible from the dependency Vm.dll relies on other libraries and files in the JRE. If you pull jvm.dll out of the same directory as WinFile.exe, you can run it, but the JNI_CREATEJAVAJVM call never fails. The workaround is to include the Jvm.dll dynamic library in the search path, as shown in the batch file above.



Once the Java virtual machine is successfully built, it is necessary to obtain the class name dynamically and get the Java method through the class name and function signature, and the method of obtaining the function signature is to run the following command.

1. <b>Java -s -p WinFile</b>

The signature of the function we defined in the Java file is output,



The rest of the matter is to be responsible for encoding the characters ' arguments, such as marking out noteworthy places.



The conversion process of the parameters is to convert the char* to UTF8 encoding and then into a jstring Java method, the return value of the Java method should be first to the jstring type, then to char* type and then to GB2312. Run the program, the output is as follows



Summarize

The programming of mixed language should pay attention to the factors of encoding transmission and language running environment. For example, to construct a Java runtime environment in C + +. Mixed language programming is difficult, but also very interesting, the advantages of both languages can be obtained, not very good things?

C + + and Java Hybrid programming

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.