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

📄 appendb.htm

📁 java书籍《thinking in java》
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<!--
This document was converted from RTF source: 
By rtftohtml 4.19
See http://www.sunpack.com/RTF
Filename:TIJ2.rtf
Application Directory:C:\TOOLS\RTF2HTML\
Subject:
Author:Bruce Eckel
Operator:Bruce Eckel
Document Comments:
Version Comments:
Comments:
Keywords:
Translation Date:05/21/2001
Translation Time:10:39:34
Translation Platform:Win32
Number of Output files:23
This File:AppendB.htm
SplitDepth=1
SkipNavPanel=1
SkipLeadingToc=1
SkipTrailingToc=1
GenContents=1
GenFrames=1
GenIndex=1
-->
<HEAD lang="en"><META http-equiv="Content-Type" content="text/html">
<TITLE>B: The Java Native Interface (JNI)</TITLE>
</HEAD>

<BODY  BGCOLOR="#FFFFFF"><DIV ALIGN="CENTER">
  <a href="http://www.MindView.net">
  <img src="mindview.gif" alt="MindView Inc." BORDER = "0"></a>
  <CENTER>
    <FONT FACE="Verdana" size = "-1">
    [ <a href="README.txt">Viewing Hints</a> ]
    [ <a href="RevHist.htm">Revision History</a> ]
    [ <a href="http://www.mindview.net/Books/TIJ/">Book Home Page</a> ]
    [ <a href="http://www.mindview.net/Etc/MailingList.html">Free Newsletter</a> ] <br>
    [ <a href="http://www.mindview.net/Seminars">Seminars</a> ]
    [ <a href="http://www.mindview.net/CDs">Seminars on CD ROM</a> ]
    [ <a href="http://www.mindview.net/Services">Consulting</a> ]
    </FONT>
  <H2><FONT FACE="Verdana">
  Thinking in Java, 2nd edition, Revision 12</FONT></H2>
  <H3><FONT FACE="Verdana">&copy;2000 by Bruce Eckel</FONT></H3>
  
    <FONT FACE="Verdana" size = "-1">
     [ <a href="AppendA.htm">Previous Chapter</a> ] 
    [ <a href="SimpCont.htm">Short TOC</a> ] 
    [ <a href="Contents.htm">Table of Contents</a> ] 
    [ <a href="DocIdx.htm">Index</a> ]
     [ <a href="AppendC.htm">Next Chapter</a> ] 
    </FONT>
    
  </CENTER>
  </P></DIV><A NAME="_Toc477690737"></A><A NAME="_Toc481064947"></A><A NAME="Heading628"></A><FONT FACE = "Verdana"><H1 ALIGN="LEFT">
B: The Java Native Interface (JNI)</H1></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia" SIZE=4><backtalk:display ID=TIJ3_APPENDIXB_I0>
</FONT><FONT FACE="Georgia"><I>The
material in this appendix was contributed by and used with the permission of
Andrea Provaglio (www.AndreaProvaglio.com).</I></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia" SIZE=4>The Java language and its standard
API are rich enough to write full-fledged applications. But in some cases you
must call <A NAME="Index2279"></A><A NAME="Index2280"></A>non-Java code; for
example, if you want to access operating-system-specific features,
<A NAME="Index2281"></A>interface with special <A NAME="Index2282"></A>hardware
devices, reuse a preexisting, non-Java code base, or implement
<A NAME="Index2283"></A>time-critical sections of code.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Interfacing with non-Java code requires
dedicated support in the compiler and in the Virtual Machine, and additional
tools to map the Java code to the non-Java code. The standard solution for
calling non-Java code that is provided by JavaSoft is called the <I>Java Native
Interface</I>, which will be introduced in this appendix. This is not an
in-depth treatment, and in some cases you&#8217;re assumed to have partial
knowledge of the related concepts and techniques.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_APPENDIXB_I0' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_APPENDIXB_I1>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">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="Index2284"></A><A NAME="Index2285"></A>native
method interface (NMI). NMI has design characteristics that make it unsuitable
for adoption across all virtual machines. For this reason, future versions of
the language might no longer support NMI, and it will not be covered here.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_APPENDIXB_I1' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_APPENDIXB_I2>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Currently, JNI is designed to interface
with native methods written only in <A NAME="Index2286"></A>C or C++. Using JNI,
your native methods can:</FONT><BR></P></DIV>
<UL>
<LI><FONT FACE="Symbol">	</FONT><FONT FACE="Georgia">Create, inspect, and update
Java objects (including arrays and
<B>String</B>s)</FONT><LI><FONT FACE="Symbol">	</FONT><FONT FACE="Georgia">Call
Java methods</FONT><LI><FONT FACE="Symbol">	</FONT><FONT FACE="Georgia">Catch
and throw
exceptions</FONT><LI><FONT FACE="Symbol">	</FONT><FONT FACE="Georgia">Load
classes and obtain class
information</FONT><LI><FONT FACE="Symbol">	</FONT><FONT FACE="Georgia">Perform
run-time type checking</FONT></UL><DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Thus,
virtually everything you can do with classes and objects in ordinary Java you
can also do in native methods. 

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_APPENDIXB_I2' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_APPENDIXB_I3>
</FONT><A NAME="_Toc481064948"></A><BR></P></DIV>
<A NAME="Heading629"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
Calling a native method</H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">We&#8217;ll start with a simple example:
a Java program that calls a native method, which in turn calls the standard C
library function <B>printf(&#160;)</B>. 

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_APPENDIXB_I3' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_APPENDIXB_I4>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The first step is to write the Java code
declaring a native method and its arguments:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: appendixb:ShowMessage.java</font>
<font color=#0000ff>public</font> <font color=#0000ff>class</font> ShowMessage {
  <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(<font color=#004488>"MsgImpl"</font>);
    <font color=#009900>// Linux hack, if you can't get your library</font>
    <font color=#009900>// path set in your environment:</font>
    <font color=#009900>// System.load(</font>
    <font color=#009900>//  "/home/bruce/tij2/appendixb/MsgImpl.so");</font>
  }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
    ShowMessage app = <font color=#0000ff>new</font> ShowMessage();
    app.ShowMessage(<font color=#004488>"Generated with JNI"</font>);
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The native method declaration is followed
by a <B>static</B> block that calls <B>System.loadLibrary(&#160;)</B> (which you
could call at any time, but this style is more appropriate).
<B>System.loadLibrary(&#160;)</B> loads a DLL in memory and links to it. The DLL
must be in your system library path. The file name extension is automatically
added by the JVM depending on the platform.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_APPENDIXB_I4' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_APPENDIXB_I5>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">In the above code you can also see a call
to the <B>System.load(&#160;)</B> method, which is commented out. The path
specified here is an absolute path, rather than relying on an environment
variable. Using an environment variable is naturally the better and more
portable solution, but if you can&#8217;t figure that out you can comment out
the <B>loadLibrary(&#160;)</B> call and uncomment this one, adjusting the path
to your own directory.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_APPENDIXB_I5' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_APPENDIXB_I6>
</FONT><A NAME="_Toc481064949"></A><BR></P></DIV>
<A NAME="Heading630"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
The header file generator: javah</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Now compile your Java source file and run
<A NAME="Index2287"></A><B>javah</B> on the resulting <B>.class</B> file,
specifying the <B>&#8212;jni</B> switch (this is done automatically for you by
the makefile in the source code distribution for this book):

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_APPENDIXB_I6' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_APPENDIXB_I7>
</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>javah &#8212;jni ShowMessage</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>javah</B> 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 <B>ShowMessage.h</B> source
file (edited slightly to fit into this book):

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_APPENDIXB_I7' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_APPENDIXB_I8>
</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><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 ShowMessage */</font>

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

#ifdef __cplusplus
}
#endif
#endif</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">As you can see by the <B>#ifdef
__cplusplus</B> preprocessor directive, this file can be compiled either by a C
or a C++ compiler. The first <B>#include</B> directive includes <B>jni.h</B>, a
header file that, among other things, defines the types that you can see used in
the rest of the file. <A NAME="Index2288"></A><B>JNIEXPORT</B> and
<A NAME="Index2289"></A><B>JNICALL</B> are macros that expand to match
platform-specific directives. <B>JNIEnv</B>, <B>jobject</B> and <B>jstring</B>
are JNI data type definitions, which will be explained shortly.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_APPENDIXB_I8' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

⌨️ 快捷键说明

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