⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tij0191.html

📁 学习java的经典书籍
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<html><body>

<table width="100%"><tr>
<td>
<a href="http://www.bruceeckel.com/javabook.html">Bruce Eckel's Thinking in Java</a>
</td>
<td align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0190.html">Prev</a> | <a href="tij0192.html">Next</a>
</td>
</tr></table>
<hr>

<H2 ALIGN=LEFT>
The
Java Native Interface
</H2>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">JNI
is a fairly rich programming interface that allows you to call native methods
from a Java application. It was added in Java 1.1, maintaining a certain degree
of compatibility with its Java 1.0 equivalent, the <A NAME="Index3089"></A><A NAME="Index3090"></A>native
method interface (NMI). NMI has design characteristics that make it unsuitable
for adoption in all virtual machines. For this reason, future versions of the
language might no longer support NMI, and it will not be covered here.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Currently,
JNI is designed to interface with native methods written only in <A NAME="Index3091"></A>C
or C++. Using JNI, your native methods can:
</FONT><P></DIV>
<UL>
<LI><FONT FACE="Symbol" SIZE=3 COLOR="Black">	</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Create,
inspect, and update Java objects (including arrays and 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">s)</FONT><LI><FONT FACE="Symbol" SIZE=3 COLOR="Black">	</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Call
Java methods
</FONT><LI><FONT FACE="Symbol" SIZE=3 COLOR="Black">	</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Catch
and throw exceptions
</FONT><LI><FONT FACE="Symbol" SIZE=3 COLOR="Black">	</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Load
classes and obtain class information
</FONT><LI><FONT FACE="Symbol" SIZE=3 COLOR="Black">	</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Perform
runtime type checking
</FONT></UL><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Thus,
virtually everything you can do with classes and objects in ordinary Java you
can also do in native methods. 
</FONT><a name="_Toc408018819"></a><P></DIV>
<A NAME="Heading586"></A><H3 ALIGN=LEFT>
Calling
a native method
</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">We&#8217;ll
start with a simple example: a Java program that calls a native method, which
in turn calls the Win32 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>MessageBox(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
API function to display a graphical text box. This example will also be used
later with J/Direct. If your platform is not Win32, just replace the C header
include: 
</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">#include
&lt;windows.h&gt;
</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">with</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">#include
&lt;stdio.h&gt;
</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">and
replace the call to 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>MessageBox(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
with a call to 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>printf(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
first step is to write the Java code declaring a native method and its arguments:
</FONT><P></DIV>

<font color="#990000"><PRE><font color="#0000ff">class</font> ShowMsgBox {
  <font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> main(String [] args) {
    ShowMsgBox app = <font color="#0000ff">new</font> ShowMsgBox();
    app.ShowMessage("Generated with JNI");
  }
  <font color="#0000ff">private</font> <font color="#0000ff">native</font> <font color="#0000ff">void</font> ShowMessage(String msg);
  <font color="#0000ff">static</font> {
    System.loadLibrary("MsgImpl");
  }
}</PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
native method declaration is followed by a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
block that calls 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>System.loadLibrary(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
(which you could call at any time, but this style is more appropriate). 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>System.loadLibrary(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
loads a DLL in memory and links to it. The DLL must be in your system path or
in the directory containing the Java class file. The file name extension is
automatically added by the JVM depending on the platform.
</FONT><P></DIV>
<A NAME="Heading587"></A><H4 ALIGN=LEFT>
The
C header file generator: javah
</H4>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Now
compile your Java source file and run <A NAME="Index3092"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>javah</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
on the resulting 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>.class</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
file. 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Javah</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
was present in version 1.0, but since you are using Java 1.1 JNI you must
specify the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>&#8211;jni</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
switch:
</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">javah
&#8211;jni ShowMsgBox
</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Javah</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
reads the Java class file and for each native method declaration it generates a
function prototype in a C or C++ header file. Here&#8217;s the output: the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>ShowMsgBox.h</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
source file (edited slightly to fit into the book):
</FONT><P></DIV>

<font color="#990000"><PRE><font color="#009900">/* DO NOT EDIT THIS FILE 
   - it is machine generated */</font>
#include &lt;jni.h&gt;
<font color="#009900">/* Header for class ShowMsgBox */</font>

#ifndef _Included_ShowMsgBox
#define _Included_ShowMsgBox
#ifdef __cplusplus
extern "C" {
#endif
<font color="#009900">/*
 * Class:     ShowMsgBox
 * Method:    ShowMessage
 * Signature: (Ljava/lang/String;)V
 */</font>
JNIEXPORT <font color="#0000ff">void</font> JNICALL 
Java_ShowMsgBox_ShowMessage
  (JNIEnv *, jobject, jstring);

#ifdef __cplusplus
}
#endif
#endif</PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">As
you can see by the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>#ifdef
__cplusplus
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
preprocessor directive, this file can be compiled either by a C or a C++
compiler. The first 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>#include</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
directive includes 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>jni.h</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
a header file that, among other things, defines the types that you can see used
in the rest of the file. <A NAME="Index3093"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>JNIEXPORT</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and <A NAME="Index3094"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>JNICALL</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
are macros that expand to match platform-specific directives; 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>JNIEnv</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>jobject</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>jstring</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
are JNI data type definitions.
</FONT><P></DIV>
<A NAME="Heading588"></A><H4 ALIGN=LEFT>
Name
mangling and function signatures
</H4>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">JNI
imposes a naming convention (called 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>name
mangling
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">)
on native methods; this is important, since it&#8217;s part of the mechanism by
which the virtual machine links Java calls to native methods. Basically, all
native methods start with the word &#8220;Java,&#8221; followed by the name of
the class in which the Java native declaration appears, followed by the name of
the Java method; the underscore character is used as a separator. If the Java
native method is overloaded, then the function signature is appended to the
name as well; you can see the native signature in the comments preceding the
prototype. For more information about name mangling and native method
signatures, please refer to the JNI documentation.
</FONT><P></DIV>
<A NAME="Heading589"></A><H4 ALIGN=LEFT>
Implementing
your DLL
</H4>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">At
this point, all you have to do is write a C or C++ source file that includes
the javah-generated header file and implements the native method, then compile
it and generate a dynamic link library. This part is platform-dependent, and
I&#8217;ll assume that you know how to create a DLL. The code below implements
the native method by calling a Win32 API. It is then compiled and linked into a
file called 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>MsgImpl.dll</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
(for &#8220;Message Implementation&#8221;).
</FONT><P></DIV>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -