📄 java如何调用c-c++(jni).htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0057)http://eps.www85.cn4e.com/java/article/devshow.asp?id=184 -->
<HTML><HEAD><title>csdn_Java如何调用C/C++(JNI)</TITLE>
<META content="text/html; charset=gb2312" http-equiv=Content-Type>
<STYLE type=text/css>TD {
FONT-FAMILY: "Verdana", "Arial", "宋体"; FONT-SIZE: 9pt
}
A {
COLOR: #660000; TEXT-DECORATION: underline
}
A:hover {
COLOR: #660000; TEXT-DECORATION: none
}
.line {
LINE-HEIGHT: 14pt
}
</STYLE>
<META content="MSHTML 5.00.2920.0" name=GENERATOR></HEAD>
<BODY bgColor=#ffffff text=#000000>
<table>
<TBODY>
<TR>
<TD height=21>
<DIV align=center><B><FONT size=3>Java如何调用C/C++(JNI)
<BR><FONT size=2> </FONT></FONT></FONT>
<HR align=center color=#cccccc noShade SIZE=1>
</DIV></TD></TR>
<TR>
<TD class=line><FONT
color=#333300>Java如何调用C/C++(JNI)<BR><BR><BR>Java开发者 jdeveloper <BR>1. program Java source file loading Native method <BR>// <BR>// <BR>// NativeDemo <BR>// Author: huang_jc 1999/05/20 <BR>// file name: NativeDemo.java <BR>public class NativeDemo <BR>{ <BR>int i; <BR>int j; <BR>public static void main(String args[]) <BR>{ <BR>NativeDemo ob = new NativeDemo(); <BR>ob.i = 10; <BR>ob.j = ob.test(); <BR>System.out.println("this is ob.i:"+ob.i+"\n"); <BR>System.out.println("this is ob.j:"+ob.j); <BR>} <BR>public native int test(); <BR>static <BR>{ <BR>System.loadLibrary("NativeDemo"); <BR>} <BR>} <BR><BR>2.Compile file NativeDemo.java <BR>Javac NativeDemo.java <BR><BR>3.Use javah.exe to produce file NativeDemo.c and NativeDemo.h <BR>a : javah NativeDemo to produce NativeDemo.h which is: <BR>/* DO NOT EDIT THIS FILE - it is machine generated */ <BR>#include <native.h> <BR>/* Header for class NativeDemo */ <BR><BR>#ifndef _Included_NativeDemo <BR>#define _Included_NativeDemo <BR><BR>#pragma pack(4) <BR><BR>typedef struct ClassNativeDemo { <BR>long i; <BR>long j; <BR>} ClassNativeDemo; <BR>HandleTo(NativeDemo); <BR><BR>#pragma pack() <BR><BR>#ifdef __cplusplus <BR>extern "C" { <BR>#endif <BR>extern long NativeDemo_test(struct HNativeDemo *); <BR>#ifdef __cplusplus <BR>} <BR>#endif <BR>#endif <BR>b : use javah -stubs NativeDemo to produce NativeDemo.c which seems as: <BR>/* DO NOT EDIT THIS FILE - it is machine generated */ <BR>#include <StubPreamble.h> <BR><BR>/* Stubs for class NativeDemo */ <BR>/* SYMBOL: "NativeDemo/test()I", Java_NativeDemo_test_stub */ <BR>__declspec(dllexport) stack_item *Java_NativeDemo_test_stub(stack_item *_P_,struct execenv *_EE_) { <BR>extern long NativeDemo_test(void *); <BR>_P_[0].i = NativeDemo_test(_P_[0].p); <BR>return _P_ + 1; <BR>} <BR>4. Write your own implimentation of method test() <BR>//file name test.c <BR>#include <StubPreamble.h> <BR>#include "nativedemo.h" <BR>#include <stdio.h> <BR><BR>long NativeDemo_test(HNativeDemo *this) <BR>{ <BR>printf("hello:!\n"); <BR>printf("this is in the native method!\n"); <BR>return (unhand(this)->i)*2; <BR>} <BR><BR>5. link NativeDemo.c and test.c to produce NativeDemo.dll <BR>Use VC++5.0 tools :Cl.exe <BR>Cl /GD NativeDemo.c test.c /LD <BR>then we get NativeDemo.dll <BR><BR><BR>NativeDemo.c <BR>test.c <BR>Generating Code... <BR><BR>/out:NativeDemo.dll <BR>/dll <BR>/implib:NativeDemo.lib <BR>NativeDemo.obj <BR>test.obj <BR>Creating library NativeDemo.lib and object NativeDemo.ex <BR>Press any key to continue <BR><BR><BR><BR><BR>6.Run <BR><BR>java NativeDemo <BR><BR>we get the such result: <BR><BR>D:\vj11user\native>java NativeDemo <BR>hello:! <BR>this is in the native method! <BR>this is ob.i:10 <BR><BR>this is ob.j:20 <BR><BR>D:\vj11user\native> <BR><BR><BR>NOTE: <BR>Do not forget this: <BR>c:> set INCLUDE=d:\java\include;d:\java\win32;%INCLUDE% <BR>C:>set LIB =d:\java\lib;%LIB% <BR>where d:\java is the directory of your jdk <BR><BR>new version jdk1.2.1 : <BR><BR>With new jdk1.2.1 : <BR><BR>1.javac NativeDemo.java <BR><BR>2. javah NativeDemo to produce head file (You need not to javah -stubs NativeDemo to produce NativeDemo.c file), <BR><BR>You don`t need the c file. Just the NativeDemo.h is sufficient! <BR><BR>3.Use VC++ produce a window dll project and add the NativeDemo.h to it. <BR><BR>then implement the function. All is OK! <BR>NOTE: the data type conversion between java and c++.See jni.h for more information.
</FONT></TD></TR>
<TR>
<TD height=5>
<HR align=center color=#cccccc noShade SIZE=1>
</TD></TR></TBODY></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -