Java calls C through JNI

Source: Internet
Author: User

Java calls C in a number of ways, this article introduces the author's recent learning process, to avoid repeating similar mistakes in the future.

First, Java must be a dynamic-link library called C, the Dll/so file compiled by the compiler.

The following describes how GCC compiles DLLs.

In general, the main functions, sub-function definitions, child function declarations (global variables, macros, etc.) are placed in separate files.

We mainly build the available DLLs for the main function to be called by the child function compilation.

There is a project in which there are three documents, respectively

#include <stdio.h>"test.h"int main (intChar * argv[]) {        printf ("2 + 3 =%d\n", sum (23));     return 0 ;}
main.c
1 #include <stdio.h>2 #include"test.h"34  int sum (intint  b)5{6     return A + b; 7 }
test.c
#ifndef _test_h_devc_ #define _test_h_devc_int sum (intint  b); #endif
test.h

If you want to call the DLL file of the TEST.C function in Main.c, you can compile the test.c dynamic link library

Install MinGW on the Windows platform to simulate the GCC compilers under Linux.

Get the Test.dll file. Then put, MAIN.C, Test.h, Test.dll in another folder, compiled to get

Executable file, Rs.exe. Running results are

The following is the Javah compiler C header file, this article mainly describes the operation of the myeclipse under the steps.

The first configuration of the Javah command is as follows:

Among them, the need to self-configuration of three parameters

Location: Is the path of your native Javah.exe. Working Directory: ${project_loc}/"${project_loc}" -jni ${java_type_name}

Once configured, you will be able to create the required header files for Java in one click.

Examples are as follows:

Project Name: Zz-testdll

1  Packagemain;2 3 ImportTestdll. Sum;4 5  Public classMain {6     Static{7System.loadlibrary ("Test");8     }9      Public Static voidMain (string[] args) {Ten         intrs =sum.msum (); One System.out.println (RS); A     } -}
Main.java
1  Package Testdll; 2 3  Public class Sum {4      Public native Static int MSum (); 5 }
Sum.java

Select the Sum.java file, and then run the configured Javah command to get the testdll_sum.h file in the

/*Do not EDIT this file-it are machine generated*/#include<jni.h>/*Header for Class Testdll_sum*/#ifndef _included_testdll_sum#define_included_testdll_sum#ifdef __cplusplusextern "C" {#endif/** class:testdll_sum * method:msum * Signature: () I*/jniexport jint jnicall java_testdll_sum_msum (jnienv*, Jclass); #ifdef __cplusplus}#endif#endif
testdll_sum.h

Here's how to implement the method in the Testdll_sum.h file

(Because the GCC compiler and VC + + compiler implementation of different ways, see http://www.blogjava.net/lixf/archive/2006/01/09/25177.html)

We need to modify the name of the method in the generated testdll_sum.h, and precede the name of the function with an underscore.

The testdll_sum.h after the change are as follows:

1 /*Do not EDIT this file-it are machine generated*/2#include <jni.h>3 /*Header for Class Testdll_sum*/4 5 #ifndef _included_testdll_sum6 #define_included_testdll_sum7 #ifdef __cplusplus8 extern "C" {9 #endifTen /* One * Class:testdll_sum A * Method:msum - * Signature: () I -  */ the jniexport jint jnicall _java_testdll_sum_msum -(JNIENV *, jclass); -  - #ifdef __cplusplus + } - #endif + #endif
testdll_sum.h

The method is implemented as follows:

" testdll_sum.h " // the first two parameters (env and obj) can be ignored jniexport jint jnicall _java_testdll_sum_msum   * env, Jclass obj)  {      printf ("Hello world!\n");      return 5 ;  }
SUM.C

In the Java include folder, get three files, altogether 5 files for compiling.

Get Test.dll and put it in the Java project's running directory.

Select Main.java file, run, get.

After I finished, I learned that Java through JNA can be very convenient to call C file, have the opportunity to learn.

The above content is for reference only.

Java calls C through JNI

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.