📄 36677.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<META NAME="Keywords" CONTENT="java,程序,JDK,Java基础知识——JNI入门介绍(上)">
<meta name="robots" content="ALL">
<META NAME="Description" CONTENT="{$PageColumn}">
<meta name="rating" content="General">
<meta name="author" content="http://www.chinaitlab.com/">
<meta name="language" content="chinese">
<title>Java基础知识——JNI入门介绍(上)|中国IT实验室</title>
<link href="http://windows.chinaitlab.com/css.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="da"><IFRAME marginWidth=0 marginHeight=0 src="http://www.chinaitlab.com/cms/top.htm" frameBorder=0 width="100%" scrolling=no height=53></IFRAME></div>
<div id="dagg"><script language="javascript" src="http://www.chinaitlab.com/AD/200604/12.js"></script></div>
<div id="tiao">
<table width="950" border="0" cellpadding="0" cellspacing="0" class="zhi12">
<tr>
<td><div align="center"><strong> <a href="http://java.chinaitlab.com" target="_blank">Java频道</a> </strong></div></td>
<td><img src="http://windows.chinaitlab.com/images/art/z002.gif"></td>
<td> <a href="http://java.chinaitlab.com/List_173.html" target="_blank">新手入门</a>
| <a href="http://java.chinaitlab.com/List_172.html" target="_blank">考试认证</a>
| <a href="http://java.chinaitlab.com/List_179.html" target="_blank">开源专区</a>
| <a href="http://java.chinaitlab.com/List_174.html" target="_blank">核心技术</a>
| <a href="http://java.chinaitlab.com/List_176.html" target="_blank">J2EE</a>
| <a href="http://java.chinaitlab.com/List_175.html" target="_blank">高级技术</a>
| <a href="http://java.chinaitlab.com/List_177.html" target="_blank">J2ME</a>
| <a href="http://java.chinaitlab.com/List_178.html" target="_blank">Java&xml</a>
| <a href="http://java.chinaitlab.com/List_180.html" target="_blank">其他技术</a>
| <a href="http://blog.chinaitlab.com/database.htm" target="_blank">博客日志</a>
| <a href="http://bbs.chinaitlab.com/list.asp?boardid=148" target="_blank">Java论坛</a></td>
<td><a href="http://rss.chinaitlab.com/ChannelRss.asp?ID=1004" target="_blank"><img src="http://windows.chinaitlab.com/images/art/z003.gif" border="0"></a></td>
</tr>
</table>
</div>
<div id="tiao2">
<table width="899" height="22" border="0" cellpadding="0" cellspacing="0" class="zhi12">
<form name=frmsearch action=http://search.chinaitlab.com/index_search.asp method=get target=_blank>
<tr>
<td width="666" height="19" valign="bottom"><font color="#FFFFFF">您现在的位置: <a class='LinkPath' href='http://www.chinaitlab.com'>中国IT实验室</a> >> <a class='LinkPath' href='http://java.chinaitlab.com/Index.html'>Java</a> >> <a class='LinkPath' href='http://java.chinaitlab.com/List_173.html'>新手入门</a> >> <a class='LinkPath' href='http://java.chinaitlab.com/List_188.html'>JDK</a> >> 文章正文</font></td>
<td width="172"> <div align="center"><input name="bigclass" type="hidden" value="1">
<input name="keywords" type="text" class="lanyu" size="25">
</td>
<td width="61"><img src="http://windows.chinaitlab.com/images/art/z010.gif" onClick=javascript:document.frmsearch.submit() width="50" height="17"></td>
</tr>
</form>
</table>
</div>
<!-- 正文 -->
<div id="tiao3">
<!-- 右边排列 -->
<div id="bian1">
<div id="biao"><B class="zhi25"><FONT color=#003399>Java基础知识——JNI入门介绍(上)</FONT></B>
<div id="xinxi">
<table width="20%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="5"></td>
</tr>
</table>
<table width="480" height="60" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#cccccc" class="zhi12">
<tr>
<td>
<table width="479" height="59" border="0" align="center" cellpadding="0" cellspacing="0" class="zhi12" bgcolor="#f7f7f7">
<tr>
<td width="238" align="left" style="padding-left:5px">
<script language='javascript' src='http://security.chinaitlab.com/JS/tech_title_safe3.js'></script></td>
<td width="3" background="http://www.chinaitlab.com/images/dian.gif"></td>
<td width="238" align="left" style="padding-left:5px">
<script language='javascript' src='http://server.chinaitlab.com/JS/tech_title_s3.js'></script></td>
</tr>
</table>
</td>
</tr>
</table>
<table width="20%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="5"></td>
</tr>
</table>
</div>
<div id="xinxi">
<div align="center">
<table border=0 align=center cellPadding=0 cellSpacing=0 class="zhi12">
<tr>
<td> <IMG height=12 src="http://www.chinaitlab.com/www/news/images/c7.gif" width=17>
ChinaITLab收集整理 <IMG height=12 src="http://www.chinaitlab.com/www/news/images/c2.gif" width=12>
2005-10-18 <IMG height=12 src="http://www.chinaitlab.com/www/news/images/c3.gif" width=13>
<A href="javascript:window.external.AddFavorite('http://java.chinaitlab.com/JDK/36677.html','Java基础知识——JNI入门介绍(上)|中国IT实验室')" target=_top>保存本文</A> <IMG height=18 src="http://www.chinaitlab.com/www/news/images/c5.gif" width=11>
<A onclick="javascript:window.open('http://www.chinaitlab.com/www/news/sendfriend.asp?id1=32580&location1='+document.all.location1.value)" href="http://java.chinaitlab.com/JDK/36677.html">推荐给好友</A> <IMG height=17 src="http://www.chinaitlab.com/www/news/images/c6.gif" width=17>
<A class=A01 style="CURSOR: hand" onclick="AddPortal('29987')">QQ上看本站</A> <IMG height=12 src="http://www.chinaitlab.com/www/news/images/c1.gif" width=13>
<A href="javascript:window.external.AddFavorite('http://www.chinaitlab.com','中国IT实验室')" target=_top>收藏本站</A>
</td>
</tr>
</table>
</div>
</div>
</div>
<hr width="700">
<DIV id="wenzhang" class="zhi14"><br><table cellSpacing=0 cellPadding=0 align=left border=0 >
<tr>
<td >
<script language="javascript" src="http://www.chinaitlab.com/AD/200604/11.js"></script></td>
</tr>
</table>
<br> 学习了一下JNI,发表文章的时候不知道该选什么好了,不知道JNI应该属于那个范畴^_^。<br> <br> <b>1.简介</b><br> <br> JNI是Java Native Interface的缩写,它的设计目的是:<br> <br> The standard Java class library may not support the platform-dependent features needed by your application.<br> <br> You may already have a library or application written in another programming language and you wish to make it accessible to Java applications<br> <br> You may want to implement a small portion of time-critical code in a lower-level programming language, such as assembly, and then have your Java application call these functions<br> <br> <b>2.JNI的书写步骤</b><br> <br> 编写带有native声明的方法的java类<br> <br> 使用javac命令编译所编写的java类<br> <br> 使用javah ?jni java类名生成扩展名为h的头文件<br> <br> 使用C/C++实现本地方法<br> <br> 将C/C++编写的文件生成动态连接库<br> <br> ok<br> <br> 1) 编写java程序:<br> <br> 这里以HelloWorld为例。<br> <br> 代码1:<br> <br> class HelloWorld {<br> public native void displayHelloWorld();<br> <br> static {<br> System.loadLibrary("hello");<br> }<br> <br> public static void main(String[] args) {<br> new HelloWorld().displayHelloWorld();<br> }<br> }<br> <br> 声明native方法:如果你想将一个方法做为一个本地方法的话,那么你就必须声明改方法为native的,并且不能实现。其中方法的参数和返回值在后面讲述。<br> <br> Load动态库:System.loadLibrary("hello");加载动态库(我们可以这样理解:我们的方法displayHelloWorld()没有实现,但是我们在下面就直接使用了,所以必须在使用之前对它进行初始化)这里一般是以static块进行加载的。同时需要注意的是System.loadLibrary();的参数“hello”是动态库的名字。<br> <br> main()方法<br> <br> 2) 编译没有什么好说的了<br> <br> javac HelloWorld.java<br> <br> 3) 生成扩展名为h的头文件<br> <br> javah ?jni HelloWorld<br> <br> 头文件的内容:<br> /* DO NOT EDIT THIS FILE - it is machine generated */<br> #include <jni.h><br> /* Header for class HelloWorld */<br> <br> #ifndef _Included_HelloWorld<br> #define _Included_HelloWorld<br> #ifdef __cplusplus<br> extern "C" {<br> #endif<br> /*<br> * Class: HelloWorld<br> * Method: displayHelloWorld<br> * Signature: ()V<br> */<br> JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld<br> (JNIEnv *, jobject);<br> <br> #ifdef __cplusplus<br> }<br> #endif<br> #endif<br> <br> (这里我们可以这样理解:这个h文件相当于我们在java里面的接口,这里声明了一个Java_HelloWorld_displayHelloWorld (JNIEnv *, jobject);方法,然后在我们的本地方法里面实现这个方法,也就是说我们在编写C/C++程序的时候所使用的方法名必须和这里的一致)。<br> <br> 4) 编写本地方法<br> <br> 实现和由javah命令生成的头文件里面声明的方法名相同的方法。<br> <br> 代码2:<br> <br> 1 #include <jni.h><br> 2 #include "HelloWorld.h"<br> 3 #include <stdio.h><br> 4 JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld(JNIEnv *env, jobject obj)<br> {<br> printf("Hello world!\n");<br> return;<br> }<br> <br> 注意代码2中的第1行,需要将jni.h(该文件可以在%JAVA_HOME%/include文件夹下面找到)文件引入,因为在程序中的JNIEnv、jobject等类型都是在该头文件中定义的;另外在第2行需要将HelloWorld.h头文件引入(我是这么理解的:相当于我们在编写java程序的时候,实现一个接口的话需要声明才可以,这里就是将HelloWorld.h头文件里面声明的方法加以实现。当然不一定是这样)。然后保存为HelloWorldImpl.c就ok了。<br> <br> 5) 生成动态库<br> <br> 这里以在Windows中为例,需要生成dll文件。在保存HelloWorldImpl.c文件夹下面,使用VC的编译器cl成。<br> <br> cl -I%java_home%\include -I%java_home%\include\win32 -LD HelloWorldImp.c -Fehello.dll<br> <br> 注意:生成的dll文件名在选项-Fe后面配置,这里是hello,因为在HelloWorld.java文件中我们loadLibary的时候使用的名字是hello。当然这里修改之后那里也需要修改。另外需要将-I%java_home%\include -I%java_home%\include\win32参数加上,因为在第四步里面编写本地方法的时候引入了jni.h文件。<br> <br> 6) 运行程序<br> <br> java HelloWorld就ok。^_^ </DIV>
<br>
<table width="96%">
<tr>
<td background="http://www.chinaitlab.com/images/dian.gif" height="3"></td>
</tr>
</table>
<br>
<DIV id="peixun">
<div id="peixun1" class="zhi12"> <a href="http://peixun.chinaitlab.com" target="_blank">相关产品与培训</a></div>
<iframe src="http://java.chinaitlab.com/peixun.htm" width="670" height="161" frameborder="0" scrolling="no" marginheight="0" marginwidth="0"></iframe>
</DIV><br><br>
<table width="675" height="48" border="0" cellpadding="0" cellspacing="0">
<tr class="zhi13">
<td width="332" height="16"><img src="http://windows.chinaitlab.com/images/art/z007.gif" width="12" height="12"> <font color="#3BA5D7">相关文章</font></td>
<td width="19" rowspan="3"> </td>
<td width="2" rowspan="3" background="http://www.chinaitlab.com/images/dian.gif"></td>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -