Use JNI

本文介绍如何通过Java Native Interface (JNI) 实现Java应用程序调用C++项目的方法。主要内容包括定义Java本地类、创建C++ DLL以及在Java中使用该DLL。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!-- /* Font Definitions */ @font-face {font-family:Wingdings; panose-1:5 0 0 0 0 0 0 0 0 0; mso-font-charset:2; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:0 268435456 0 0 -2147483648 0;} @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:Verdana; panose-1:2 11 6 4 3 5 4 4 2 4; mso-font-charset:0; mso-generic-font-family:swiss; mso-font-pitch:variable; mso-font-signature:536871559 0 0 0 415 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0in; margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体;} h1 {mso-style-next:Normal; margin-top:12.0pt; margin-right:0in; margin-bottom:3.0pt; margin-left:0in; mso-pagination:widow-orphan; page-break-after:avoid; mso-outline-level:1; font-size:16.0pt; font-family:Arial; mso-font-kerning:16.0pt;} p.MsoBodyTextIndent, li.MsoBodyTextIndent, div.MsoBodyTextIndent {margin:0in; margin-bottom:.0001pt; text-indent:15.0pt; mso-pagination:widow-orphan; font-size:16.0pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体;} @page Section1 {size:595.3pt 841.9pt; margin:1.0in 1.25in 1.0in 1.25in; mso-header-margin:35.4pt; mso-footer-margin:35.4pt; mso-paper-source:0;} div.Section1 {page:Section1;} /* List Definitions */ @list l0 {mso-list-id:665786640; mso-list-type:hybrid; mso-list-template-ids:1869888120 67698689 67698691 67698693 67698689 67698691 67698693 67698689 67698691 67698693;} @list l0:level1 {mso-level-number-format:bullet; mso-level-text:; mso-level-tab-stop:51.0pt; mso-level-number-position:left; margin-left:51.0pt; text-indent:-.25in; font-family:Symbol;} @list l1 {mso-list-id:1164513590; mso-list-type:hybrid; mso-list-template-ids:-47439314 67698703 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;} @list l1:level1 {mso-level-tab-stop:51.0pt; mso-level-number-position:left; margin-left:51.0pt; text-indent:-.25in;} @list l2 {mso-list-id:1185437698; mso-list-type:hybrid; mso-list-template-ids:246322210 67698703 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;} @list l2:level1 {mso-level-tab-stop:51.0pt; mso-level-number-position:left; margin-left:51.0pt; text-indent:-.25in;} ol {margin-bottom:0in;} ul {margin-bottom:0in;} -->

 

                                   谭炳超

For web development, we need to use Java app to call C++ project. We can use JNI to realize this requirement.

 

 

 

 

As the above diagram show, we have C++ PROJECT PAG that uses C++ to develop the functions. At the web interface, we use Java to develop the web function.

Because different compilers have different methods to rename function .So we cannot use C++ Project by Java directly.

 

In order to realize the call between JAVA and C++, we have to define a project in Java IDE as JAVA NATIVE CLASS PAG. The JAVA Native Class announces all class includes the main functions provided C++ PROJECT.

 

Main steps:

1.     Write JAVA Class the have native announce. We call it JAVA NATIVE CLASS PAG. 

 

2.    Use  javac or IDE(JBuilder,eclipse)to compile Java class

 

3.    Use  javah to create suitable head files

 

4.    Create a JAVA,C++ PROJECT  in VC6 to output a dll. The project will realize the head files

 

5.    Write a main function to use the native class.

 

I give out a simple example:

 

 

1.TestJniClass

1.    TestJniClass include two files: TestJniClass.java and MakeVFile.java.

 

·       MakeVFile.java code :

package   cellcom;

 

public   class   MakeVFile

{

public   static   void   main(String[]   args)

{

        TestJniClass jdes=new TestJniClass();

       

        int a= jdes.EraseMedia(111);

 

        int b=jdes.Test666(2222);

 

      System.out.println("result1 = "+a);

 

      System.out.println("result2 = "+b);

 

}

}

·       TestJniClass.java code:

 

package   cellcom;

public class TestJniClass

{

    static{System.loadLibrary("JniDll");}

    public native int EraseMedia(int index);

 

    public native int Test666(int index);

 

}

 

I put two files in D:/cellcom.

Use cmd : D:/>javah -verbose -d d:/cellcom -classpath ;  cellcom.TestJniClass

 

To create cellcom TestJniClass.h.

 

TestJniClass.h code:

 

/* DO NOT EDIT THIS FILE - it is machine generated */

#include <jni.h>

/* Header for class cellcom_TestJniClass */

 

#ifndef _Included_cellcom_TestJniClass

#define _Included_cellcom_TestJniClass

#ifdef __cplusplus

extern "C" {

#endif

/*

 * Class:     cellcom_TestJniClass

 * Method:    EraseMedia

 * Signature: (I)I

 */

JNIEXPORT jint JNICALL Java_cellcom_TestJniClass_EraseMedia

  (JNIEnv *, jobject, jint);

 

/*

 * Class:     cellcom_TestJniClass

 * Method:    Test666

 * Signature: (I)I

 */

JNIEXPORT jint JNICALL Java_cellcom_TestJniClass_Test666

  (JNIEnv *, jobject, jint);

 

#ifdef __cplusplus

}

#endif

#endif

 

They all are C style function. When java loadLibrary("JniDll"),it can know how to call them. C style functions do not rename function in dll.

2.JniDLL Project

We create a dynamic-link library project in VC6.

Add cellcom_TestJniClass.h to project.

Add create cellcom_TestJniClass.cpp to realize the function.

 

Add system path change VCtools/options/directories

settings, add the follow paths:

%java_home%/includeI

%java_home%/include/win32

 

At this project, we can call other C++ function.

 

At last, compile this project and give out a JniDll.dll.

 

3.Use JAVA Native class

 

create a project in eclipse as test1.

Put the JniDll.dll in the project folder. We can use it .

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值