In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about how to talk about the mixed programming of C++ and Java. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
The reality is that real projects usually involve multiple programming languages. To cite a few simple examples, a software may use Delphi or VB as the interface development language for rapid development. The underlying instructions or core algorithms will be used to process data. When data processing is involved, scripting languages such as Javascript or Python will be used for data analysis and processing for security and rapid development. Therefore, developers should learn or master language mixed programming. C++ and Java are the two mainstream programming languages, but now there is little information on the realization of mixed programming in these two languages, but it is incomplete, and sometimes a variety of questions are now ambiguous, causing great trouble to friends who are learning or using these two languages. My bad work, I hope to use these two languages mixed programming learning friends can throw a brick to attract jade.
Realization principle
The technology used to realize the interaction between Java and C++ is called JNI (Java Native Interface). The program written by C++ can be called by Java program as long as the interface generated by JNI is realized, while the program written by Java, called by C++, needs to run the Java virtual machine and call the method realized by Java through JNI query.
Environment variable setting
The version of Java used in this article is (build 1.6.0_03-b05), and the version of C++ is VC++ version 6.0. And set the following environment variables according to your local Java and C++ installation directory
Be careful not to lack the include and lib red lines of Java (the batch file cpp-env.Bat in the source package file).
Set the environment variable for Java, as shown in the following figure
Pay attention to this part of the red line, which is very important when C++ calls the Java method (the batch file java-env.Bat in the source package file). After running these two batch files in command mode, you can run the demo program in command mode.
The method of calling C++ by Java
This file of WinFile.java in the% SRC%/Java-cpp directory in the source file (the Java syntax states that the class name must be the same as the file name) defines a WinFile class with the following contents
In line 18 of the code, declare a method GetFilesFromDir with the native attribute, which passes in a character class parameter and returns a character class parameter, while System.loadLibrary loads the specified shared link library, and the dynamic library loaded as shown in the parameter is libwinfile.dll. On the windows platform, the suffix .dll is automatically added when executed. Run the following command in command mode:
The * command generates a compilation file of WinFile.class, while the second command generates a header file of WinFile.h, which contains the definition of the native method in WinFile.java.
In the language definition of String +, the String of Java language is defined as jstring. Note that the characters of Java language and the encoding of programs are realized by UTF-8 coding, so the Chinese characters in Java will be displayed as garbled if there is no code conversion in the C++ method. Similarly, if you return Chinese characters to Java in C++ 's method, if you don't convert the character encoding to UTF-8, it will also be garbled in Java's method.
The above is the% SRC%/Java-cpp/WinFile.cpp part of the code, the code implements two functions, one is to convert UTF-8 to GB2312, the other is to convert GB2312 to UTF-8, and jni.h this header file also provides the conversion function between jstring and char* types.
GetStringUTFChars NewStringUTF
Run the following compile command:
Cl-GX-LD WinFile.cpp-FelibWinFile.dll
Then generate the dynamic library libWinFile.dll (note that the generated name is the same as the name of the parameters in the System.loadLibrary function) and run the Java class.
The output is as follows
C++ calls Java class methods
This demonstrates the method returned by a call to String as a parameter, as do other types of method calls.
Create a statically declared Java method
This method will take an incoming character parameter from C++ and return the character class of Java, allowing the C++ function to output the content. The code is located at SRC%/cpp-java/WinFile.java
The file is compiled to generate a file with java bytecode, which must be shipped on JVM. For C++ to hold these Java bytecodes, you must run JVM. The code that runs JVM is located in the file% SRC%/cpp-java/WinFile.cpp, as shown in the following figure.
Through the JNI_CreateJavaJVM function, C++ will run JVM, note that the generated WinFile.exe file prompts the need for jvm.dll, but do not copy jvm.dll from the jre directory to the WinFile.exe directory, because jvm can function properly, you must rely on jre's java library and other dynamic libraries, although you can't see from dependency that jvm.dll depends on other libraries and files in jre. If jvm.dll is pulled out and located in the same directory as WinFile.exe, although it can be run, the JNI_CreateJavaJVM call will always fail. The solution is to add the dynamic library jvm.dll to the search path, as shown in the batch file above.
After successfully establishing the Java virtual machine, you need to dynamically obtain the class name and obtain the Java method through the class name and function signature, which is obtained by running the following command.
Java-s-p WinFile
Then the signature of the function we defined in the Java file is output
The rest is to be responsible for encoding the parameters of the characters, as noted in the following figure
The conversion process of the parameter is to convert char* to UTF8 encoding and then to jstring Wei into java method, and the return value of java method should also be converted to jstring type first, then to char* type and then to GB2312. Run the program, and the output is as follows
Mixed language programming should pay attention to the factors of coding transmission and language running environment. For example, to construct the running environment of Java in C++. Programming in mixed languages is difficult, but it's also interesting. Isn't it good that you can get the advantages of both languages?
The above is the editor to share with you how to talk about C++ and Java mixed programming, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.