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

📄 appendixa.html

📁 java 是一个很好的网络开发环境。由于它是通过解释的方法
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<!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:Tjava14.rtf
Application Directory:c:\TOOLS\RTF2HTML\
Subject:
Author:Bruce Eckel
Operator:Bruce Eckel
Document Comments:
Version Comments:
Comments:
Keywords:
Translation Date:02/04/2000
Translation Time:23:26:24
Translation Platform:Win32
Number of Output files:27
This File:AppendixA.html
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>A: Using non-Java code</TITLE>
</HEAD>

<BODY  BGCOLOR="#FFFFFF"><DIV ALIGN="CENTER">
  <a href="http://www.MindView.net">
  <img src="mindview-head.gif" alt="MindView Inc." BORDER = "0"></a>
  <CENTER>
    <FONT FACE="Verdana" size = "-1">
    [ <a href="README-HTML.txt">Viewing Hints</a> ]
    [ <a href="http://www.mindview.net/TIJ2/index.html">2nd Edition</a> ]
    [ <a href="http://www.mindview.net/MailingList.html">Free Newsletter</a> ] <br>
    [ <a href="http://www.mindview.net/Training.html">Seminars</a> ]
    [ <a href="http://www.mindview.net/javaCD2.html">Seminars on CD ROM</a> ]
    [ <a href="http://www.mindview.net/CPPServices/#ConsultingServices">Consulting</a> ]
    </FONT>
  <H2><FONT FACE="Verdana">
  Thinking in Java, 1st edition</FONT></H2>
  <H3><FONT FACE="Verdana">&copy;1998 by Bruce Eckel</FONT></H3>
  
    <FONT FACE="Verdana" size = "-1">
     [ <a href="Chapter17.html">Previous Chapter</a> ] 
    [ <a href="SimpleContents.html">Short TOC</a> ] 
    [ <a href="Contents.html">Table of Contents</a> ] 
    [ <a href="DocIndex.html">Index</a> ]
     [ <a href="AppendixB.html">Next Chapter</a> ] 
    </FONT>
    
  </CENTER>
  </P></DIV><A NAME="_Toc407441462"></A><A NAME="_Toc408018817"></A><A NAME="Heading583"></A><FONT FACE = "Verdana"><H1 ALIGN="LEFT">
A: Using non-Java code</H1></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Verdana" SIZE=4>This appendix was
contributed by and used with the permission of Andrea Provaglio
(<I>www.AndreaProvaglio.com</I>).</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The Java language and its standard
API are rich enough to write full-fledged applications. But in some cases you
must call <A NAME="Index3061"></A><A NAME="Index3062"></A>non-Java code; for
example, if you want to access operating-system-specific features,
<A NAME="Index3063"></A>interface with special <A NAME="Index3064"></A>hardware
devices, reuse a pre-existing, non-Java code base, or implement
<A NAME="Index3065"></A>time-critical sections of code. 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.
(There&#8217;s also a simple approach: in Chapter 15, the section titled
&#8220;a Web application&#8221; contains an example of connecting to non-Java
code using standard input and output.) Currently, different vendors offer
different solutions: Java 1.1 has the
<A NAME="Index3066"></A><A NAME="Index3067"></A>Java Native Interface (JNI),
Netscape has proposed its <A NAME="Index3068"></A>Java Runtime Interface, and
Microsoft offers <A NAME="Index3069"></A>J/Direct,
<A NAME="Index3070"></A><A NAME="Index3071"></A>Raw Native Interface (RNI), and
<A NAME="Index3072"></A><A NAME="Index3073"></A>Java/COM
integration.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">This fragmentation among different
vendors implies serious drawbacks for the programmer. If a Java application must
call native methods, the programmer might need to implement different versions
of the native methods depending on the platform the application will run on. The
programmer might actually need different versions of the Java code as well as
different Java virtual machines.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Another solution is
<A NAME="Index3074"></A>CORBA (Common Object Request Broker Architecture), an
integration technology developed by the OMG (Object Management Group, a
non-profit consortium of companies). CORBA is not part of any language, but is a
specification for implementing a common communication bus and services that
allow interoperability among objects implemented in different languages. This
communication bus, called an <A NAME="Index3075"></A><A NAME="Index3076"></A>ORB
(Object Request Broker), is a product implemented by third-party vendors, but it
is not part of the Java language specification.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">This appendix gives an overview of
JNI, J/Direct, RNI, Java/COM integration, and CORBA. 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. But in the end, you should be able to
compare the different approaches and choose the one that is most appropriate to
the problem you want to solve.</FONT><A NAME="_Toc408018818"></A><BR></P></DIV>
<A NAME="Heading584"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
The Java Native Interface</H2></FONT>
<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="Index3077"></A><A NAME="Index3078"></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><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Currently, JNI is designed to
interface with native methods written only in <A NAME="Index3079"></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
runtime 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. </FONT><A NAME="_Toc408018819"></A><BR></P></DIV>
<A NAME="Heading585"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Calling a native method</H3></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
Win32 <B>MessageBox(&#160;)</B> 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><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>#include &lt;windows.h&gt;</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">with</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>#include &lt;stdio.h&gt;</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">and replace the call to
<B>MessageBox(&#160;)</B> with a call to <B>printf(&#160;)</B>.</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=#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(<font color=#004488>"Generated with JNI"</font>);
  }
  <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>);
  }
}</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 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><BR></P></DIV>
<A NAME="Heading586"></A><FONT FACE = "Verdana"><H4 ALIGN="LEFT">
The C header file generator: javah</H4></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Now compile your Java source file
and run <A NAME="Index3080"></A><B>javah</B> on the resulting <B>.class</B>
file. <B>Javah</B> was present in version 1.0, but since you are using Java 1.1
JNI you must specify the <B>&#8211;jni</B> switch:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>javah &#8211;jni ShowMsgBox</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>ShowMsgBox.h</B> source
file (edited slightly to fit into the book):</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 ShowMsgBox */</font>

#ifndef _Included_ShowMsgBox
#define _Included_ShowMsgBox
#ifdef __cplusplus
extern <font color=#004488>"C"</font> {
#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></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="Index3081"></A><B>JNIEXPORT</B> and
<A NAME="Index3082"></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.</FONT><BR></P></DIV>
<A NAME="Heading587"></A><FONT FACE = "Verdana"><H4 ALIGN="LEFT">
Name mangling and function signatures</H4></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">JNI imposes a naming convention
(called <I>name mangling</I>) 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><BR></P></DIV>
<A NAME="Heading588"></A><FONT FACE = "Verdana"><H4 ALIGN="LEFT">
Implementing your DLL</H4></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">At this point, all you have to do

⌨️ 快捷键说明

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