服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

java如何调用c/c++(jni)


  java如何调用c/c++(jni)


java开发者 jdeveloper
1. program java source file loading native method
//
//
// nativedemo
// author: huang_jc 1999/05/20
// file name: nativedemo.java
public class nativedemo
{
int i;
int j;
public static void main(string args[])
{
nativedemo ob = new nativedemo();
ob.i = 10;
ob.j = ob.test();
system.out.println("this is ob.i:"+ob.i+"");
system.out.println("this is ob.j:"+ob.j);
}
public native int test();
static
{
system.loadlibrary("nativedemo");
}
}

2.compile file nativedemo.java
javac nativedemo.java

3.use javah.exe to produce file nativedemo.c and nativedemo.h
a : javah nativedemo to produce nativedemo.h which is:
/* do not edit this file - it is machine generated */
#include <native.h>
/* header for class nativedemo */

#ifndef _included_nativedemo
#define _included_nativedemo

#pragma pack(4)

typedef struct classnativedemo {
long i;
long j;
} classnativedemo;
handleto(nativedemo);

#pragma pack()

#ifdef __cplusplus
extern "c" {
#endif
extern long nativedemo_test(struct hnativedemo *);
#ifdef __cplusplus
}
#endif
#endif
b : use javah -stubs nativedemo to produce nativedemo.c which seems as:
/* do not edit this file - it is machine generated */
#include <stubpreamble.h>

/* stubs for class nativedemo */
/* symbol: "nativedemo/test()i", java_nativedemo_test_stub */
__declspec(dllexport) stack_item *java_nativedemo_test_stub(stack_item *_p_,struct execenv *_ee_) {
extern long nativedemo_test(void *);
_p_[0].i = nativedemo_test(_p_[0].p);
return _p_ + 1;
}
4. write your own implimentation of method test()
//file name test.c
#include <stubpreamble.h>
#include "nativedemo.h"
#include <stdio.h>

long nativedemo_test(hnativedemo *this)
{
printf("hello:!");
printf("this is in the native method!");
return (unhand(this)->i)*2;
}

5. link nativedemo.c and test.c to produce nativedemo.dll
use vc++5.0 tools :cl.exe
cl /gd nativedemo.c test.c /ld
then we get nativedemo.dll


nativedemo.c
test.c
generating code...

/out:nativedemo.dll
/dll
/implib:nativedemo.lib
nativedemo.obj
test.obj
creating library nativedemo.lib and object nativedemo.ex
press any key to continue




6.run

java nativedemo

we get the such result:

d:vj11userative>java nativedemo
hello:!
this is in the native method!
this is ob.i:10

this is ob.j:20

d:vj11userative>


note:
do not forget this:
c:> set include=d:javainclude;d:javawin32;%include%
c:>set lib =d:javalib;%lib%
where d:java is the directory of your jdk

new version jdk1.2.1 :

with new jdk1.2.1 :

1.javac nativedemo.java

2. javah nativedemo to produce head file (you need not to javah -stubs nativedemo to produce nativedemo.c file),

you don`t need the c file. just the nativedemo.h is sufficient!

3.use vc++ produce a window dll project and add the nativedemo.h to it.

then implement the function. all is ok!
note: the data type conversion between java and c++.see jni.h for more information.

扫描关注微信公众号