1java program calls local C ++ through JNI. First example _ helloworld

Source: Internet
Author: User
javacode:TestNative.java
 
Package COM. cloud; public class testnative {public static void main (string [] ARGs) {system. loadlibrary ("nativetestcodedll"); New testnative (). sayhello ();}/*** write a Java program. The method must be modified using native, and the method cannot have an implementation body. * Call the [Native> Local] C/C ++ Program */Public native void sayhello ();}

Generate the C ++ header file: com_cloud_testnative.h

 

/* DO NOT EDIT THIS FILE - it is machine generated */#include <jni.h>/* Header for class com_cloud_TestNative */#ifndef _Included_com_cloud_TestNative#define _Included_com_cloud_TestNative#ifdef __cplusplusextern "C" {#endif/* * Class:     com_cloud_TestNative * Method:    sayHello * Signature: ()V */JNIEXPORT void JNICALL Java_com_cloud_TestNative_sayHello  (JNIEnv *, jobject);#ifdef __cplusplus}#endif#endif

 

 

 

 

 

 

Compile a Java program to call C and C ++ programs through JNI

1. Write a Java program and compile a declaration to call the implementation method of C ++. The method must be modified with native, and the method cannot have an implementation body.
2. Run the JDK javah command to switch to the directory of the class file or specify the classpath.
3. Enter the full Class Name of the javah Java file in the CMD command line to generate the C ++ header file [. h file] corresponding to the class file ]. The. h file generated under the class directory indicates that the header file is successfully generated.
4. Use C ++ ide vs2010 to create C ++ project Win32 Win32 console application, enter the project name, And next select DLL> select an empty project. Then generate the project.
5. Run the javah command to generate the header file 【. h: copy the file to the C ++ project, select the header file in Vs, right-click the file, add existing items, and select the copied header file 【. H ].
6. Select source file add new item C ++ file (. cpp ). Enter the file name to create the file.
7. Open the copied header file and copy it to the created. cpp file.
8. Write c ++ code
Example:
# Include "com_cloud_testnative.h"
# Include <iostream>
Using namespace STD;
Jniexport void jnicall java_com_cloud_testnative_sayhello (jnienv * ENV, jobject OBJ)
{
Cout <"Hello word" <Endl;
}

9 compile. An error occurs.
The first prompt indicates that JNI is missing. h file, set JNI under JDK \ include. copy the H file to the C ++ project and change the brackets to double quotation marks. Example: # include "JNI. h"
The second prompt is missing # include "jni_md.h". Find jni_md.h in JDK \ include \ Win32 and copy it to the C ++ project.
Compiled. The DLL file of the C ++ project is generated. OK.
10. Set the DLL dynamic link library path to the environment variable path.
11 compile a Java program to call the DLL dynamic Connection Library. In Java, the file name of system. loadlibrary ("teatnative"); DLL does not contain the extension. Load the testnative. dll file.
If an error occurs, you need to restart eclipse so that the environment variables will be reloaded when eclipse is started.
12. Execute the call and output helloworld. Success !!!.

 

 

 

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.