📄 ch10.htm
字号:
<HTML><HEAD><TITLE>developer.com - Reference</TITLE><META NAME="Author" CONTENT="developer.com"><META NAME="Keywords" CONTENT="javascript, ActiveX, VRML, Push, channel,push channel, applet, directory, news, community, developer, internet,intranet, java, java applet, J++, javabeans, java products, tools, javatools, JDK, java development kit, java development environment, JIT,JavaPlan, enterprise tools, JVM, Java Virtual Machine, Java resources, API,browser, java documentation, CDF, Common Desktop Format, RDF, ResourceDescription Framework, Netcaster, Netcast, Castanet, meta-language, metadata,solaris, Solaris, SUN, CGI, Perl, Oracle, database, network, Netscape API,Apache, apache, SSL, mSQL, Netscape, Fasttrack, FastTrack,industry standard, ActiveX bridge, web, internet, site, sitedesign, microsoft, active server pages, asp, iis, nt, win95, 95, html, cgi,frontpage, business, corporate, vbscript, javascript, activex, isapi,database, access, sql, server, www, 3.0, 3.01, 3.02, 3.03, 3.04, 4.0,4.0b2, 4.01, 4.03, 4.04, connection, online, commerce, publisher,publishing, publish, webmaster, dynamic html, dhtml, windows 98, netscape,netscape software, shareware, magazines, jobs, japan, free, freeware, freestuff"><META NAME="description" CONTENT="Developer.com is the leading onlineservice for developers. It includes Gamelan, the official directory for Java, as wellas news, information, tutorials, and directories for other Internet technologiesincluding ActiveX, JavaScript, perl, VRML, Java Beans, push technologies, andother Internet and intranet technologies. Developer.com is also the home ofDeveloper Direct, the Professional Developer's Store, where developers canpurchase and download thousands of Web, authoring and other developmentproducts at some of today's lowest prices."><META NAME="distribution" CONTENT="global"><META NAME="resource-type" CONTENT="document"><META NAME="language" CONTENT="en"></HEAD><BODY BACKGROUND="/images/curve_r-wide.gif" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#DD0000" VLINK="#999999" ALINK="#FF0000"><TABLE><TR><TD VALIGN=TOP WIDTH=120><A HREF="/" ><IMG SRC="/images/masthead_r.gif" WIDTH="100" HEIGHT="78" BORDER="0"ALT="Developer.com Logo"></a></TD><TD><a target="resource window" href="http://adserver.developer.com/cgi-bin/accipiter/adclick.exe/AREA=DCBAN.REF/DT=null"><img src="http://adserver.developer.com/cgi-bin/accipiter/adserver.exe/AREA=DCBAN.REF/DT=null" HEIGHT="60" WIDTH="468" ALT="Click here to support our advertisers"><BR><CENTER>Click here to support our advertisers</CENTER></a></TD></TR></TABLE><!--outer table--><TABLE><!--outer table--><TR><!--outer table--><TD VALIGN="TOP"><!--navbar info --><table><tr><td><A HREF="http://www.developerdirect.com/" ><IMG SRC="/images/button_s.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><b>SHOPPING</b></font></a></td></tr><tr><td><A HREF="/community/jobs/c_jobs.html"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>JOB BANK</B></font></a></td></tr><tr><td><A HREF="http://www.classifieds2000.com/cgi-cls/display.exe?Developer+computer+searchcomputercategories"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>CLASSIFIEDS</B></font></a></td></tr><tr><td><A HREF="/directories/directories.html"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>DIRECTORIES</B></font></a></td></tr><tr><td><A HREF="/reference/"><IMG SRC="/images/button_r.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>REFERENCE</B></font></a><br><a href="/reference/r_library.html"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000">Online Library</a></font></td></tr><tr><td><A HREF="/classroom/"><IMG SRC="/images/button_r.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>LEARNING CENTER</B></font></a></td></tr><tr><td><A HREF="/journal/"><IMG SRC="/images/button_n.gif" width="15" height="11" BORDER="0"><B><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000">JOURNAL</font></B></a></td></tr><tr><td><A HREF="/news/"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><b>NEWS CENTRAL</b></font></a></td></tr><tr><td><A HREF="/downloads/"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><b>DOWNLOADS</b></font></a></td></tr><tr><td><A HREF="/community/" ><IMG SRC="/images/button_c.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>COMMUNITY</B></font></a></td></tr><tr><td><A HREF="/calendar/"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><b>CALENDAR</b></font></a></td></tr><tr><td><A HREF="/about/"><IMG SRC="/images/button_a.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><b>ABOUT US</b></a></font></td></tr></table><table><tr><td><form action="/subscribe/list.cgi" method=post ><img src="/images/rule.gif" width="100" height="2"><br><B><FONT FACE="Arial, Helvetica" SIZE="-1">Journal:</b></font><br><input name='email' value='your e-mail' size="11"><input type='hidden' name='state' value='Subscribe'></td></tr><tr><td><input type='submit' value='Subscribe'><br><font size="-1">Get the weekly email highlights from the most popular journal for developers!<br><a href="http://www.developer.com/subscribe.html">Current issue</a><img src="/images/rule.gif" width="100" height="2"><input type='hidden' name='name' value='New Subscriber'></FORM></td></tr></table><!--/navbar--><font size="-1"><A HREF="http://www.developer.com">developer.com</a><BR><A HREF="http://www.developerdirect.com">developerdirect.com</a><BR><A HREF="http://www.htmlgoodies.com">htmlgoodies.com</a><BR><A HREF="http://www.javagoodies.com">javagoodies.com</a><br><A HREF="http://www.jars.com">jars.com</a><BR><a href="http://www.intranetjournal.com">intranetjournal.com</a><br><a href="http://www.javascripts.com">javascripts.com</a><br></font><!--outer table--></TD><!--outer table--><TD VALIGN="TOP"><BLOCKQUOTE><P><img src="/images/header_r.gif" width="440" height="50"><P><MAP NAME ="header_r2.gif.map"><AREA SHAPE ="RECT" HREF ="/reference/author.html" COORDS ="361,0,440,29"><AREA SHAPE ="RECT" HREF ="/reference/basic.html" COORDS ="254,0,360,29"><AREA SHAPE ="RECT" HREF ="/reference/help.html" COORDS ="183,0,251,29"><AREA SHAPE ="RECT" HREF ="/reference/cool.html" COORDS ="100,0,181,29"><AREA SHAPE ="RECT" HREF ="/reference/mybook.html" COORDS ="0,0,100,29"></MAP><MAP NAME ="header_r1.gif.map"><AREA SHAPE ="RECT" HREF ="/reference/personalize.html" COORDS ="339,0,439,29"></MAP><p><font face="Arial,Helvetica" size="-1" color="#006666"><A HREF="/reference/r_library.html"><B>All Categories</A> :</B><b><A HREF="/reference/r_java.html">Java</A></b></font><p> <META NAME="GENERATOR" CONTENT="Mozilla/3.0b5aGold (WinNT; I) [Netscape]"><H1><FONT COLOR=#FF0000>Chapter 10</FONT></H1><H1><B><FONT SIZE=5 COLOR=#FF0000>Native Methods and Java</FONT></B></H1><P><HR WIDTH="100%"></P><P><H3 ALIGN=CENTER><FONT COLOR="#000000"><FONT SIZE=+2>CONTENTS<A NAME="CONTENTS"></A></FONT></FONT></H3><UL><LI><A HREF="#DecidingtoUseNativeMethods" >Deciding to Use Native Methods</A><LI><A HREF="#NativeMethodsfromtheJavaSide" >Native Methods from the Java Side</A><LI><A HREF="#WritingNativeMethods" >Writing Native Methods</A><UL><LI><A HREF="#UsingJavah" >Using Javah</A><LI><A HREF="#JavaArrays" >Java Arrays</A><LI><A HREF="#TheStubsCode" >The Stubs Code</A></UL><LI><A HREF="#ChapterProjectADatabaseInterfaceLi" >Chapter Project: A Database Interface Library Using ODBC</A><UL><LI><A HREF="#CallingBackIntoJava" >Calling Back Into Java</A><LI><A HREF="#ConstructingJavaObjectsfromC" >Constructing Java Objects from C</A><LI><A HREF="#CreatingtheLibrary" >Creating the Library</A></UL><LI><A HREF="#DatabaseServer" >Database Server</A><LI><A HREF="#AddingPacketAssemblytoDGTP" >Adding Packet Assembly to DGTP</A><UL><LI><A HREF="#TheElectionServer" >The Election Server</A></UL><LI><A HREF="#ElectionClient" >Election Client</A><LI><A HREF="#Summary" >Summary</A></UL><HR><P>This chapter builds on the concepts of <A HREF="ch9.htm" >Chapter 9</A>,"Java Socket Programming," to construct a sophisticateddatabase server. The server reads from a back-end database inreal time. Since the standard Java classes do not, as yet, providea database interface, native methods must be used to read thetables. A native interface library is developed and used to reada sample database. Along the way, you will learn the following:<UL><LI><FONT COLOR=#000000>Calling C methods from Java</FONT><LI><FONT COLOR=#000000>Creating C libraries that can be calledfrom Java</FONT><LI><FONT COLOR=#000000>Handling Java types as arguments to Cfunctions</FONT><LI><FONT COLOR=#000000>Accessing Java class members from C functions</FONT><LI><FONT COLOR=#000000>Calling Java methods from C functions</FONT><LI><FONT COLOR=#000000>Throwing Java exceptions from a C function</FONT></UL><H2><A NAME="DecidingtoUseNativeMethods"><FONT SIZE=5 COLOR=#FF0000>Decidingto Use Native Methods</FONT></A></H2><P>The decision to use native methods comes with a heavy cost. Anyclasses that load native methods cannot be used in an applet!There aren't any browsers available that allow applets to callnative methods. The reason for this restriction is security. TheJava security manager can't protect against malicious attacksfrom within a native method. The only solution is to not allownative methods to be called.<P>Another disadvantage is the lost portability of your applications.One of the chief benefits of using Java is the portability ofthe resulting code between disparate platforms. A small industryhas developed trying to provide truly portable application frameworks.For all their refinement, you are still left recompiling a versionfor each platform. Java steps into the fray with an intermediateformat that enables you to compile once and execute everywhere.When you choose to use native methods, you lose this capability.Once again, you will be relegated to coding a separate libraryfor each platform that runs Java.<P>Now that the downside to native methods is clear, why use themat all? The single best reason to resort to native methods isto add functions not present in the standard classes. Maybe youwant to interface with a specific piece of hardware or use a newnetwork driver. Whatever the reason, native methods supply thecapability. Because Java is portable, it cannot take advantageof operating specific features. The Java developers endeavoredto supply the standard classes with all needed functionality,but this is an impossible task. The ability to call native C methodssupplies a way to use features not available through the Javaclasses. Most of the functions in the standard classes themselveshave to resort to native method calls to accomplish their tasks.<H2><A NAME="NativeMethodsfromtheJavaSide"><FONT SIZE=5 COLOR=#FF0000>NativeMethods from the Java Side</FONT></A></H2><P>Native methods within a Java class are very simple. Any Java methodcan be transformed into a native method-simply delete the methodbody, add a semicolon at the end, and prefix the <TT>native</TT>keyword. The following Java method<BLOCKQUOTE><TT>public int myMethod(byte[] data)<BR>{<BR> ...<BR>}</TT></BLOCKQUOTE><P>becomes<BLOCKQUOTE><TT>public native int myMethod(byte[] data);</TT></BLOCKQUOTE><P>Where does the method body get implemented? In a Java-called nativelibrary that gets loaded into Java at runtime. The class of theabove method would have to cause the library to be loaded. Thebest way to accomplish the load is to add a static initializerto the class:<BLOCKQUOTE><TT>static<BR>{<BR> System.loadLibrary("myMethodLibrary");<BR>}</TT></BLOCKQUOTE><P>Static code blocks are executed once by the system when the classis first introduced. Any operations may be specified, but libraryloading is the most common use. If the static block fails, theclass will not be loaded. This ensures that no native methodsare executed without the underlying libraries.<P>That's all there is to Java-side native methods. All the complexity
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -