On the Java JNI native method implementation

A recent question on a class data structure to solve the maze of requirements on the stack structure best with a graphical interface, you will find a maze of paths displayed. Originally intended, together with the C language to write even a graphical interface, but the C language graphical interface if you do not call the operating system's graphics library is really hard to see, so I chose to write Java graphical interface, such a hybrid programming algorithms written in C language programs. Here not concrete realization of the algorithm and C of Java graphical interface and explained, introduced only "partially connected" Java and C's.

 

The premise of using the local method

Although Java is indeed the efficiency has been greatly improved, but once encountered a large number of computing operations, or execute faster than C language. In this case, we can consider a key part of the algorithm to the C language to deal with, while the rest is handled by Java.

Java section

Java, C language to deal with the intended function declarations, where the requirement to use the native keyword, such as:

public static native void something();

With this statement native way to inform JVM definition of this method and the specific implementation is completed on the outside, Wake JVM is only responsible for the C function; after compiling this document. And after all operations are complete, but also add a domain to mark static file, such as:

static{ System.loadLibrary(“Something”); }

And then recompile. A total of around compiled twice.

Part C

As for Part C, because it is the JVM to invoke a C program, it must first ensure that JVM can identify the C program - but also because the JVM can identify the Java format, so your C code to be written in accordance with the style JVM can recognize.

Here we use javah this program, this program helps us to automatically generate C header file that contains the C function declarations JVM expectations styles in accordance with the first method compiled before the local class file, then we want to copy this function declaration header file to their C source file, as the entrance of the C program (equivalent to the normal main function in C code).

After after this so-called "main" function to handle the rest of the other C function if the program does not involve some parameters interact with Java, then it is entirely written in a C language standard.

 

Join operation

For Java is concerned, it runs on the JVM to achieve cross-platform capability. But for the C language, but it is closely dependent on the operating system. So the results Java and C mixed programming is affected by the C language, Java's cross-platform capabilities are limited - which is why the JVM itself is platform limitations, and runs on the JVM Pure Java application is cross-platform. Since receipt of the operating system constraints, we had to separate Windows and Linux.

For Linux, after obtaining the header files, use:

gcc -fPIC -I jdkPath/include -I jdkPath/include/linux -shared -o libname.so name.c

This command is compiled. After you add the path where the .so file to the library path.

For Windows, after obtaining the header files, although it can be processed by the compiler cl (Microsoft compiler), but are used to a graphical IDE for people who might command line interface is not cold. It describes using a graphical method of generating a dynamic library file IDE herein.

1) You need to copy jdk Java installation directory under / include in jni.h and jdk / include / win32 jni_md.h to include in the directory under the directory corresponding C editor. include directory can be found directly in Visual Studio, in Dev C ++ in MinGW64 in this subdirectory.

After establishing 2) a DLL using the editor program, the compiler generates a file name.dll C in this project, to copy the dll the Eclipse (if the file Eclipse) corresponding to the Java project folder; if not, may be copied to the jdk / bin or jre / bin.

 

But whether Windows or Linux, then have to go back to the appropriate local Java class source file project, said before adding static tag field, where Something is the name of your dll file. Then compile Java code to run.

 

Guess you like

Origin www.cnblogs.com/fusiji/p/11409864.html