Implementation and steps of calling the DLL file written in C (C ++) in Java

Source: Internet
Author: User

After reading this document from the Internet, I felt that it was not very difficult and novel. After all, I had very few projects to develop C language. now I have reprinted this article and added some of my notes.

JNI TipsThis article provides practical examples, steps, and guidelines for Java local methods on a 32-bit Windows platform. The example in this article uses Java Development Kit (JDK) version 1.4.1 created by Sun Microsystems. The local code written in C language is compiled and generated by Microsoft Visual C ++ 6.0 compiler. Introduction recently, as a result of project requirements, Vc has a unique advantage in image conversion to implement image conversion on Web pages. We first use VC to encapsulate the DLL for image conversion, then call the DLL for image conversion using the Java localization method JNI, and finally call the DLL generated by JNI with JavaBean. I have gained a lot through searching for information and searching for myself on the Internet over the past few days. The summary is as follows, which will reduce the number of people in this field. I. java Part 1. no package: instance 1: Public class mynative {static {system. loadlibrary ("mynative");} public native static void helloword (); Public native static string ctojava ();} Note:1) In a Java program, first declare the called library name system. loadlibrary (string libname) in the class, and locate the library in the library search path. The specific operations on the locating database depend on the operating system. In Windows, first search for the current directory, and then find the directory listed in the "path" environment variable. If the database cannot be found, unsatisfiedlinkerror is thrown. 2) The DLL generated by JNI is loaded here, instead of the name of the other generated DLL. Here, you do not need to write the extension name of the Library. The system determines whether the extension name is DLL or so. 3) you also need to make a local Declaration on the method to be called. The keyword is native. In addition, you only need to declare it, instead of implementing it. The implementation is implemented in C and will be explained later. 4) if static is added, the static method is used. If this parameter is not added, it indicates a general method. The parameters in the generated header file are different. I will explain it later. Compile it now: Use javac mynative. h to compile it and generate the corresponding class file. With javah mynative, the corresponding mynative. h header file is generated. The rest is done by the VC (we use VC to implement the corresponding C implementation part ). 2. if a package exists: instance 2: Package Com .. mynative; public class mynative {static {system. loadlibrary ("mynative");} public native static void helloword (); Public native static string ctojava ();} others are the same as above, that is, they are different when javac and javah are used. Pay attention to this when there is a package. At the beginning, my program never runs successfully, and the problem lies here. Javac./COM/mynative. Java javah com. mynative. The above sentence does not need to be explained. The following is an explanation: the first of this class is the package name. The header file is com. mynative. mynative. h. In this case, the header file I generated using javah mynative is always mynative. h. When I checked the information on the Internet, I saw someone else's header file name hitting that long, my short. But I don't know why. Now everyone knows why, just like me. :). Sometimes the path is required. For more information, see javah syntax. II. the C implementation part is the mynative generated by javah mynative. the content of the H header file is as follows:/* Do not edit this file-it is machine generated */# include <JNI. h>/* Header for class mynative */# ifndef _ included_mynative # DEFINE _ included_mynative # ifdef _ cplusplus extern "C" {# endif/** class: mynative * method: helloword * Signature: () V */jniexport void jnicall java_mynative_helloword (jnienv *, jclass);/** class: mynative * method: Ctojava * Signature: () ljava/lang/string; */jniexport jstring jnicall java_mynative_ctojava (jnienv *, jclass); # ifdef _ cplusplus} # endif next, is how to implement it. In fact, what is made with JNI is also a DLL, which is called by Java. In actual implementation, we only care about two function prototypes: jniexport void jnicall java_mynative_helloword (jnienv *, jclass); and jniexport jstring jnicall struct (jnienv *, jclass ); now let's start the exciting first step :). In the project, select Win32 dynamic-link library, and then click Next. The remaining values are taken by default. If the default value is not used, the dllmain () function is available. This function is not available if the DLL project is null. What I get here is null. Select New-> file-> C ++ source file to generate an empty *. cpp file. We named it mynative. Copy jniexport void jnicall java_mynative_helloword (jnienv *, jclass); and jniexport jstring jnicall java_mynative_ctojava (jnienv *, jclass) to the CPP file. Then include the header file. Generated mynative. CPP content: # include <stdio. h> # include "mynative. H "jniexport void jnicall java_mynative_helloword (jnienv * ENV, jclass jobject) {printf (" Hello word! /N ");} jniexport jstring jnicall java_mynative_ctojavajnienv * ENV, jclass OBJ) {jstring jstr; char STR [] =" Hello, word! /N "; jstr = env-> newstringutf (STR); Return jstr;} note the following before compiling. Note: You must copy the header files in the include folder in the SDK (and the header files in the Win32 folder below it) to the include folder in the VC. You can also set it in tools/options/directories of VC to include the header file. A bit of explanation for the Program: 1) Didn't I mention the difference between adding static and not just a parameter. It is the difference between jclass and jobject without static. That is, jniexport void jnicall java_mynative_helloword (jnienv * ENV, jobject OBJ ). 2) Here, both jniexport and jnicall are JNI keywords, indicating that this function is called by JNI. Jstring is a type of communication between the Java string type and the local string using JNI as the intermediary. We can ignore it and use it as a string (see table 1 for details ). The function name is composed of Java _, the package path of the Java program, and the function name (see the case of a package ). Parameters, 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. 3) newstringutf () is a JNI function that creates a new jstring object from a char array containing UTF-8 encoded characters. 4) The above program snippet jstr = env-> newstringutf (STR); is written in C ++ without using the Env pointer. Because the C ++ version of The jnienv function contains directly inserted member functions, they are responsible for searching function pointers. For C, the format should be changed to: jstr = (* env)-> newstringutf (ENV, STR); because all JNI function calls use the Env pointer, it is the first parameter of any local method. The env pointer is a pointer to a function pointer table. Therefore, prefix (* env)-> is added before each JNI function access to ensure that function pointers are indirectly referenced. When transferring values between C and Java programming languages, you need to understand the relationship between these value types in these two languages. These are all included in the header file JNI. h, and the price classes of these classes on the target platform are declared using the typedef statement. The header file also defines constants such as jni_false = 0 and jni_true = 1. The table describes the ing between Java and C types. Table 1 Java and C Types
Java programming language C Programming Language Bytes
Boolean Jboolean 1
Byte Jbyte 1
Char Jchar 2
Short Jshort 2
Int Jint 4
Long Jlong 8
Float Jfloat 4
Double Jdouble 8
Compile the program. Select build> rebuild all to compile the program. Click build-> build mynative. DLL to generate the DLL file. You can also use the command line CL for compiling. For more information, see other books. Emphasize again (once a headache for this thing): Place DLL 1) the current directory. 2) place it in the path indicated by path. 3) set a path in the PATH environment variable. Note that the path to be guided should be. DLL file Level 1. DLL. Next let's test the DLL we wrote (assuming that the DLL has been correctly placed ). Public class mytest {public static void main (string [] ARGs) {mynative A = new mynative ();. helloword (); system. out. println (. ctojava ();} note that mynative. put the class in the same directory as mytest. java in the same path. Compile and run mytest now. Is the output in the DOS window Hello word! Hello, world! The above is a simple C program called through the JNI method. But it is much more complicated than this. Especially when calling other DLL through JNI, there are many points to note. Now let's start to discuss the situation of package inclusion. The steps are the same as above, but they are a little different. Let's look at a function. Jniexport void jnicall java_com_mynative_mynative_helloword (jnienv * ENV, jclass jobject) {printf ("Hello word! /N ");} Let's observe the function name. The function name is composed of Java _, the package path of the Java program, and the function name. This sentence should be understood now. We also write a program to test the inclusion of the package. Program omitted. Javac./COM/mynative/mytest. Java mytest does not display the same content in the DOS window :).

 

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.