jniexamp.html
来自「jdbc书」· HTML 代码 · 共 627 行 · 第 1/2 页
HTML
627 行
<PRE>
javac ReadFile.java
</PRE>
Next, you need to generate
a header file with the native method declaration and implement
the native method to call the C functions for loading
and reading a file.
<A NAME="gen"></A>
<H3>Generate the Header File</H3>
To generate a a header file, run the <CODE>javah</CODE> command on
the <CODE>ReadFile</CODE> class. In this example, the generated
header file is named <CODE>ReadFile.h</CODE>. It provides a method
signature that you have to use when you
implement the <CODE>loadfile</CODE> native function.
<PRE>
javah -jni ReadFile
</PRE>
<A NAME="sig"></A>
<H3>Method Signature</H3>
The <CODE>ReadFile.h</CODE> header file defines the interface to map the
Java language method to the native C function. It uses a method signature to
map the arguments and return value of the Java language
<CODE>mappedfile.loadFile</CODE> method to the <CODE>loadFile</CODE> native
method in the <CODE>nativelib</CODE> library. Here is the
<CODE>loadFile</CODE> native method mapping (method signature):
<PRE>
/*
* Class: ReadFile
* Method: loadFile
* Signature: (Ljava/lang/String;)[B
*/
JNIEXPORT jbyteArray JNICALL Java_ReadFile_loadFile
(JNIEnv *, jobject, jstring);
</PRE>
The method signature parameters function as follows:
<UL>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><CODE>JNIEnv *</CODE>: A pointer to the JNI environment.
This pointer is a handle to the current thread in the Java virtual
machine, and
contains mapping and other hosuekeeping information.</FONT>
<P>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><CODE>jobject</CODE>: A reference to the method that
called this native code. If the calling method is static, this parameter would
be type <CODE>jclass</CODE> instead of <CODE>jobject</CODE>.</FONT>
<P>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><CODE>jstring</CODE>: The parameter supplied to the native method.
In this example, it is the name of the file to be read. </FONT>
</UL>
<A NAME="impl"></A>
<H3>Implement the Native Method</H3>
In this native C source file, the <CODE>loadFile</CODE> definition is a
copy and paste of the C declaration contained in <CODE>ReadFile.h</CODE>.
The definition is followed by the native method implementation.
JNI provides a mapping for both C and C++ by default.
<PRE>
JNIEXPORT jbyteArray JNICALL Java_ReadFile_loadFile
(JNIEnv * env, jobject jobj, jstring name) {
caddr_t m;
jbyteArray jb;
jboolean iscopy;
struct stat finfo;
const char *mfile = (*env)->GetStringUTFChars(
env, name, &iscopy);
int fd = open(mfile, O_RDONLY);
if (fd == -1) {
printf("Could not open %s\n", mfile);
}
lstat(mfile, &finfo);
m = mmap((caddr_t) 0, finfo.st_size,
PROT_READ, MAP_PRIVATE, fd, 0);
if (m == (caddr_t)-1) {
printf("Could not mmap %s\n", mfile);
return(0);
}
jb=(*env)->NewByteArray(env, finfo.st_size);
(*env)->SetByteArrayRegion(env, jb, 0,
finfo.st_size, (jbyte *)m);
close(fd);
(*env)->ReleaseStringUTFChars(env, name, mfile);
return (jb);
}
</PRE>
<P>
You can approach calling an existing C function instead of implementing one,
in one of two ways:
<OL>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">Map the name generated by JNI to the existing C function name.
The <A HREF="jniref.html#add">Language Issues</A>
section shows how to map between Xbase database functions and Java language code </FONT>
<P>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">Use the shared stubs code available from the
<A HREF="http://java.sun.com/products/jdk/faq/jnifaq.html">JNI page</A>
on the java.sun.com web site.</FONT>
</OL>
<A NAME="sol"></A>
<H3>Compile the Dynamic or Shared Object Library</H3>
The library needs to be compiled as a dynamic or shared object library
so it can be loaded at runtime. Static
or archive libraries are compiled into an executable and cannot be loaded
at runtime. The shared object or dynamic library for the
<CODE>loadFile</CODE> example is compiled as follows:
<PRE>
<STRONG>Gnu C/Linux:</STRONG>
gcc -o libnativelib.so -shared -Wl,-soname,libnative.so
-I/export/home/jdk1.2/
include -I/export/home/jdk1.2/include/linux nativelib.c
-static -lc
Gnu C++/Linux with Xbase
g++ -o libdbmaplib.so -shared -Wl,-soname,libdbmap.so
-I/export/home/jdk1.2/include
-I/export/home/jdk1.2/include/linux
dbmaplib.cc -static -lc -lxbase
</PRE>
<PRE>
<STRONG>Win32/WinNT/Win2000</STRONG>
cl -Ic:/jdk1.2/include
-Ic:/jdk1.2/include/win32
-LD nativelib.c -Felibnative.dll
</PRE>
<A NAME="run"></A>
<H3>Run the Example</H3>
To run the example, the Java virtual machine needs to be able to find
the native library. To do this, set the library path to the
current directory as follows:
<P>
<PRE>
<STRONG>Unix or Linux:</STRONG>
LD_LIBRARY_PATH=`pwd`
export LD_LIBRARY_PATH
<STRONG>Windows NT/2000/95:</STRONG>
set PATH=%path%;.
</PRE>
With the library path properly specified for your platform,
invoke the program as you normally would with the interpreter
command:
<PRE>
java ReadFile
</PRE>
<P>
_______<BR>
<A NAME="TJVM"><SUP>1</SUP></A> As used on this web site,
the terms "Java virtual
machine" or "JVM" mean a virtual machine
for the Java platform.
<P ALIGN="RIGHT">
<FONT SIZE="-1">[<A HREF="#top">TOP</A>]</FONT>
</FONT>
</TD>
</TR>
</TABLE>
<!-- ================ -->
<!-- End Main Content -->
<!-- ================ -->
</TD>
</TR>
</TABLE>
<!-- Copyright Insert -->
<BR CLEAR="ALL">
<FORM ACTION="/cgi-bin/search.cgi" METHOD="POST">
<TABLE WIDTH="100%" CELLPADDING="0" BORDER="0" CELLSPACING="5">
<TR>
<TD VALIGN="TOP">
<P ALIGN=CENTER>
<FONT SIZE="-1" COLOR="#999999" FACE="Verdana, Arial, Helvetica, sans-serif">
[ This page was updated: <!-- new date --> 13-Oct-99 ]</font></P>
</TD>
</TR>
<TR>
<TD BGCOLOR="#CCCCCC">
<IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD>
<CENTER>
<FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="http://java.sun.com/products/">Products & APIs</A> |
<A HREF="/developer/index.html">Developer Connection</A> |
<A HREF="/developer/infodocs/index.shtml">Docs & Training</A> |
<A HREF="/developer/support/index.html">Online Support</A><BR>
<A HREF="/developer/community/index.html">Community Discussion</A> |
<A HREF="http://java.sun.com/industry/">Industry News</A> |
<A HREF="http://java.sun.com/solutions">Solutions Marketplace</A> |
<A HREF="http://java.sun.com/casestudies">Case Studies</A>
</FONT>
</CENTER>
</TD>
</TR>
<TR>
<TD BGCOLOR="#CCCCCC">
<IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD ALIGN="CENTER">
<FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="http://java.sun.com/docs/glossary.html">Glossary</A> -
<A HREF="http://java.sun.com/applets/">Applets</A> -
<A HREF="http://java.sun.com/docs/books/tutorial/">Tutorial</A> -
<A HREF="http://java.sun.com/jobs/">Employment</A> -
<A HREF="http://java.sun.com/nav/business/">Business & Licensing</A> -
<A HREF="http://java.sun.com/javastore/">Java Store</A> -
<A HREF="http://java.sun.com/casestudies/">Java in the Real World</A>
</FONT>
</TD>
</TR>
<TR>
<TD>
<CENTER>
<FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
<a href="/siteinfo/faq.html">FAQ</a> |
<a href="/feedback/index.html">Feedback</a> |
<a href="http://www.dynamicdiagrams.net/mapa/cgi-bin/help.tcl?db=javasoft&dest=http://java.sun.com/">Map</a> |
<A HREF="http://java.sun.com/a-z/index.html">A-Z Index</A>
</FONT>
</CENTER>
</TD>
</TR>
<TR>
<TD>
<TABLE WIDTH="100%" CELLPADDING="0" BORDER="0" CELLSPACING="0">
<TR>
<TD WIDTH="50%">
<FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
For more information on Java technology<BR>
and other software from Sun Microsystems, call:<BR>
</FONT>
<FONT SIZE="-1" FACE="Verdana, Arial, Helvetica, sans-serif">
(800) 786-7638<BR></FONT>
<FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
Outside the U.S. and Canada, dial your country's
<A HREF="http://www.att.com/business_traveler/attdirecttollfree/">AT&T Direct Access Number</A> first.<BR>
</FONT>
</TD>
<TD ALIGN="RIGHT" WIDTH="50%">
<A HREF="http://www.sun.com"><IMG SRC="/images/lgsun.gif" width="64" height="30" border="0" ALT="Sun Microsystems, Inc."></A><BR>
<FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
Copyright © 1995-99
<A HREF="http://www.sun.com">Sun Microsystems, Inc.</A><BR>
All Rights Reserved.
<a href="http://www.sun.com/share/text/SMICopyright.html">Legal Terms</a>.
<A HREF="http://www.sun.com/privacy/">Privacy Policy</A>.
</FONT>
</TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
</FORM>
<!-- End Copyright Insert -->
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?