Implement Java and C language interface

xiaoxiao2021-03-06  63

Step 1: Disclaimer local variables

For example: Class nativehello {

Public native void nativehelloWorld ();

STATIC {

System.loodlibrary ("nativetest"); // Call Nativeest.dll library file

}

}

Step 2: Generate head file

First compile nativehello.java with Javac, then generate C's header files .h files with Javah.h file

Step 3: Generate root files

The command is as follows: javah -stubs nativehello (generated nativehello.c file)

Step 4: Write a C program (assuming the file named nativeest.c here)

#include

#include // Refers to the second step generation .h file

#include // Refers to the file under the JDK's Include

Void nativehello_nativehelloworld (struct hnativehello * this) {

.........

} /

* Function name nativeHello_nativeHelloWorld cannot be arbitrarily specified, can be found in the header file generated from Javah, or name the following method: Class name _ local party name (Struct H name * this) * /

Step 5: Compile DLL files

Compile Nativeest.c and NativeHello.c into DLL library files, file names are the same as the file name in System.Loodlibrary ("Nativeest")

Finally, let's talk about the test method, the source file is as follows:

Class option {

Public static void main (String [] args) {

Nativehello nh = new nativehello ();

nh.nativeHelloWorld ();

}

}

Http://java.sun.com/j2se/1.3/docs/tooldocs/solaris/javah.html

转载请注明原文地址:https://www.9cbs.com/read-88657.html

New Post(0)