Implement Java and C language interface

Source: Internet
Author: User
The first step: declaring the local variable

<CCID_NOBR>
<CCID_CODE> For example: Class nativehello{public   native void Nativehelloworld (); static{system.loodlibrary (" Nativetest ");//Call NativeTest.dll library file}}</ccid_code>
</CCID_NOBR>

Step two: Generate header files
First compile the Nativehello.java with Javac, then use Javah to generate the header files of C. h files

Step three: Generate Root files
Commands are as follows: Javah-stubs Nativehello (Generate NATIVEHELLO.C file)

Step Fourth: Write a C program (this assumes the file name is nativetest.c)

<CCID_NOBR>
<CCID_CODE> #include <stdio.h> #include <nativehello.h>//refers to the #include <stubpreamble.h of the. h file generated in the second step. >//refers to the file void Nativehello_nativehelloworld (struct Hnativehello *this) {...} under the JDK's include. /</ccid_code>
</CCID_NOBR>

* Function name Nativehello_nativehelloworld cannot be arbitrarily specified, can be found in the header file generated by Javah, or you may also use the following method name: Class Name _ local method name (struct H class name *this) * *

Step Fifth: Compile the DLL file

Compile nativetest.c and nativehello.c into a DLL library file with the same name as the file in System.loodlibrary ("Nativetest")

Finally, the test method, the source file is as follows:

<CCID_NOBR>
<ccid_code>class usenative{public static void Main (String []args) {Nativehello nh=new Nativehello (); Nh.nativehelloworld (); }}</ccid_code>
</CCID_NOBR>


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.