<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>JavaChen on JavaNative</title>
	<atom:link href="http://www.javachen.com/tag/native/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.javachen.com</link>
	<description>Just some sharing about Java open source and life</description>
	<lastBuildDate>Fri, 03 Feb 2012 03:33:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Java Native Method</title>
		<link>http://www.javachen.com/2010/01/java-native-method/</link>
		<comments>http://www.javachen.com/2010/01/java-native-method/#comments</comments>
		<pubDate>Wed, 27 Jan 2010 03:03:07 +0000</pubDate>
		<dc:creator>JavaChen</dc:creator>
				<category><![CDATA[Java Core]]></category>
		<category><![CDATA[Native]]></category>

		<guid isPermaLink="false">http://www.javachen.com/?p=938</guid>
		<description><![CDATA[阅读java.lang.reflect包内的Array类，发现了其类中有一些标注了native的方法，该方法没有方法体并以分号结尾，觉得有点奇怪，于是网上搜了一些关于java中native关键字的用法。 关于java的Native Method使用说明，在网上找到了一篇广为流传转载的文章：http://ghsea.javaeye.com/blog/72543。该文章由英文翻译，讲解的通俗易懂，想看中文的讲解的可以去上面的url观摩。在这里贴出英文原文，由于不知道英文原文出处，所以无法注明英文文章来源，但还是要感谢英文原作者。 下面是原英文文章，呵呵，很久没有读英文读物了，大概还是看得懂的。 CONTENTS What Is a Native Method? Uses for Native Methods Benefits and Trade-Offs How Does This Magic Work? Summary The goal for this chapter is to introduce you to Java&#8217;s native methods. If you are new to Java, you may not know what native methods are, and even if you are [...]<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="5"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="优化java代码效率" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F06%2Foptimize-the-efficiency-of-java-code%2F&from=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava-native-method%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">优化java代码效率</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="JavaScript和Java的区别" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2009%2F09%2Fjavascript_compareto_java%2F&from=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava-native-method%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">JavaScript和Java的区别</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="java中关于时间日期操作的常用函数" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2009%2F12%2Fjava-date-operation-function%2F&from=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava-native-method%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">java中关于时间日期操作的常用函数</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Java Enumeration" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F03%2Fjava-enumeration%2F&from=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava-native-method%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Java Enumeration</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Java StringUtil" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava-stringutil%2F&from=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava-native-method%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Java StringUtil</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="5" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>阅读java.lang.reflect包内的Array类，发现了其类中有一些标注了native的方法，该方法没有方法体并以分号结尾，觉得有点奇怪，于是网上搜了一些关于java中native关键字的用法。<br />
关于java的Native Method使用说明，在网上找到了一篇广为流传转载的文章：<a href="http://ghsea.javaeye.com/blog/72543" target="_blank">http://ghsea.javaeye.com/blog/72543</a>。该文章由英文翻译，讲解的通俗易懂，想看中文的讲解的可以去上面的url观摩。在这里贴出英文原文，由于不知道英文原文出处，所以无法注明英文文章来源，但还是要感谢英文原作者。</p>
<p>下面是原英文文章，呵呵，很久没有读英文读物了，大概还是看得懂的。<span id="more-938"></span></p>
<h3><span style="color: #000000;"><span style="font-size: x-small;">CONTENTS<a name="CONTENTS"></a> </span></span></h3>
<ul>
<li><a href="http://www.javachen.com/2010/01/java-native-method/#WhatIsaNativeMethod">What  Is a Native Method?</a></li>
<li><a href="http://www.javachen.com/2010/01/java-native-method/#UsesforNativeMethods">Uses  for Native Methods</a></li>
<li><a href="http://www.javachen.com/2010/01/java-native-method/#BenefitsandTradeOffs">Benefits  and Trade-Offs</a></li>
<li><a href="http://www.javachen.com/2010/01/java-native-method/#HowDoesThisMagicWork">How  Does This Magic Work?</a></li>
<li><a href="http://www.javachen.com/2010/01/java-native-method/#Summary">Summary</a></li>
</ul>
<hr />The goal for this chapter is to introduce you to Java&#8217;s native  methods. If  you are new to Java, you may not know what native methods  are, and even if you  are an experienced Java developer, you may not  have had a reason to learn more  about native methods. At the conclusion  of this chapter you should have a better  understanding of what native  methods are, when and why you may want to use them,  and the  consequences of using them. You should also have a basic understanding   of how native methods work. You will then be more than ready to tackle  the next  three chapters, which dive into the nitty-gritty details of  Java&#8217;s Native  Methods.</p>
<h2><a name="WhatIsaNativeMethod"><strong><span style="color: #ff0000; font-size: large;">What  Is a Native  Method?</span></strong></a></h2>
<p>Simply put, a native method is the Java interface to non-Java code.  It is  Java&#8217;s link to the “outside world.” More specifically, a native  method is a Java  method whose implementation is provided by non-Java  code, most likely C (see  Figure 30.1). This feature is not special to  Java. Most languages provide some  mechanism to call routines written in  another language. In C++, you must use the  <tt>extern "C" </tt><span style="font-family: MCPdigital-I; font-size: xx-small;">stmt</span> to signal that  the C++  compiler is making a call to C functions. It is common to see the   qualifier <tt>pascal</tt> in many C compilers to signal that the calling   convention should be done in a Pascal convention, rather than a C  convention.  FORTRAN and Pascal have similar facilities, as do most  other languages.</p>
<p><strong>Figure 30.1 : </strong><em>A  native method is a Java method  whose implementation is provided by  non-java code.</em></p>
<p>In Java, this is done via native methods. In your Java class, you  mark the  methods you wish to implement outside of Java with the <tt>native</tt> method  modifier-much like you would use the<tt> public</tt> or <tt>static</tt> modifiers. Then, rather than supplying the method&#8217;s body, you simply  place a  semicolon in its place. As an example, the following class  defines a variety of  native methods:</p>
<blockquote><p><tt>public class IHaveNatives<br />
{<br />
</tt> <tt> native  public void  Native1( int x ) ;<br />
native static public long Native2()   ;<br />
</tt> <tt> native synchronized private float Native3( Object o )   ;<br />
native void Native4( int[] ary ) throws Exception ;<br />
}</tt></p></blockquote>
<p>This sample class shows a number of possible native methods. As you  may have  noticed, native methods look much like any other Java method,  except a single  semicolon is in the place of the method body.  Naturally, the body of the method  is implemented outside of Java. What  you basically define is the interface into  this <em>external</em> method.  This method declaration describes the Java view of  some foreign code.</p>
<p>The only thing special about this declaration is that the keyword  <tt>native</tt> is used as a modifier. Every other Java method modifier can be  used  along with <tt>native</tt>, except <tt>abstract</tt>. This is logical,   because the <tt>native</tt> modifier implies that an implementation  exists, and  the <tt>abstract</tt> modifier insists that there is no  implementation. Your  native methods can be static methods, thus not  requiring the creation of an  object (or instance of a class). This is  often convenient when using native  methods to access an existing  C-based library. Naturally, native methods can  limit their visibility  with the <tt>public</tt>, <tt>private</tt>, <tt>private  protected</tt>,  <tt>protected</tt>, or unspecified <em>default </em>access. Native   methods can also be <tt>synchronized</tt> (<a href="http://www.80x86.cn/ch7.htm">see Chapter 7</a>,  “Concurrency and  Synchronization”). In the case of a <tt>synchronized</tt> native  method, the Java VM will perform the monitor locking prior to entering   the native method implementation code. So, as in Java, the developer is  not  burdened with doing the actual monitor locking and unlocking.</p>
<p>The example uses a variety (although not all) of types. This is  because a  native method can be passed any Java type. There is no  special procedure within  the Java code to pass data to the native  method. However, the developer of  native methods must be careful that  his native methods behave properly when  manipulating Java datatypes.  Native methods do not undergo the same kinds of  checking as a Java  method, and they can easily corrupt a Java datatype if care  is not  taken (<a href="http://www.80x86.cn/ch31.htm">see Chapter 31</a>, “The  Native Method  Interface”).</p>
<p>A native method can accept and return any of the Java types-including  class  types. Of course, the power of exception handling is also  available to native  methods. The implementation of the native method  can create and throw exceptions  similar to a Java method. When a native  method receives complex types, such as  class types (such as <tt>Object</tt> in the example) or array types (such as the  <tt>int[]</tt> in the  example), it has access to the contents of those types.  However, the  method used to access the contents may vary depending on the Java   implementation being used. The major point to remember is that you can  access  all the Java features from your native implementation code, but  it may be  implementation-dependent and will surely not be as convenient  or easy as it can  be done from Java.</p>
<p>The presence of native methods does not affect how other classes call  those  methods. The caller does not even realize it is calling a native  method, so no  special code is generated, and the calling convention is  the same as for any  other method-the calling depends on the method  being virtual or static. The Java  virtual machine will handle all the  details to make the call in the native  method implementation. One minor  exception may be with the methods marked with  the <tt>final</tt> modifier. The Java implementation may take advantage of a  <tt>final</tt> method and choose to inline its code. It would be doubtful that  this  could be achieved with a native <tt>final</tt> method, but this is an   optimization issue, not one of functionality. When a class containing  native  methods is subclassed, the subclass will inherit the native  method and also will  have the capability of overriding the native  method-even with a Java method  (that is, the overridden method can be  implemented in Java). If a native method  is also marked with the <tt>final</tt> modifier, a subclass is still prevented  from overriding it.</p>
<p>Native methods are very powerful, because they effectively extend the  Java  virtual machine. In fact, your Java code already uses native  methods. In the  current implementation from Sun, native methods are  used in many places to  interface to the underlying operating system.  This enables a Java program to go  beyond the confines of the Java  Runtime. With native methods, a Java program can  virtually do any  application level task.</p>
<h2><a name="UsesforNativeMethods"><strong><span style="color: #ff0000; font-size: large;">Uses  for Native  Methods</span></strong></a></h2>
<p>Java is a wonderful language to use. However, there are times when  you either  must interface with existing code, can&#8217;t express the task in  Java, or need the  absolute best performance.</p>
<h3><strong>Accessing Outside the Java Environment</strong></h3>
<p>There are times where a Java application (or applet) <em>must</em> communicate  with the environment outside of Java. This is, perhaps, the  main reason for the  existence of native methods. For starters, the  Java implementation will need to  communicate with the underlying  system. That underlying system may be an  operating system such as  Solaris or Win32, or it may be a Web browser, or it may  be custom  hardware, such as a PDA, Set-top-device, and so forth. Regardless of   what is under Java, there must be a mechanism to communicate with that  system.  At some point in a Java program, there will be that point where  Java meets the  outside world, an interface between Java and non-Java  worlds. Native methods  provide a simple clean approach to providing  this interface without burdening  the rest of the Java application with  special knowledge.</p>
<h4><strong>Accessing the Operating System</strong></h4>
<p>The Java virtual machine describes a system that the Java program can  rely on  to be there. This virtual machine supports the Java Language  and its runtime  library. It may be composed of an interpreter or can be  libraries linked to  native code. Regardless of its form, it is not a  complete system and often  relies on an existing system underneath to  provide a lot of support. More than  likely, a full-fledged operating  system, such as Solaris or Win32, resides  beneath it. The use of native  methods enables the Java Runtime to be written in  Java yet have access  to the underlying operating system, or even the parts of  the Java  virtual machine that may be written in a language such as C. Further,   if a Java feature does not encapsulate an operating system feature  needed by an  application, native methods can be used to access this  feature.</p>
<h4><strong>Embedded Java</strong></h4>
<p>It is conceivable to see a Java virtual machine embedded inside  another  program. Several WWW browsers come to mind. Perhaps this  enclosing program is  not implemented in Java. The Java Runtime may need  to access the enclosing  program for services to support the Java  environment. Once again, native methods  provide a clean interface for  this access to the surrounding program.  Furthermore, the vendor of the  program may wish to expose some features of the  program to a Java  applet. The vendor would simply need to create a set of Java  classes  containing native methods, which provide the interface for the Java   application into the program. The native method implementation would  then be the  “glue” between the Java applet and the internals of the  enclosing program.</p>
<h4><strong>Custom Hardware</strong></h4>
<p>Another important possible application of native methods being used  to access  a non-Java world is providing Java programs access to custom  hardware. Perhaps a  Java virtual machine is running within a PDA or  Set-Top-Device. A lot of what  would normally be in an operating system  may exist in hardware or software  embedded in ROM, or other custom chip  sets. Another possibility is that a  computer may be equipped with a  dedicated graphics card. It would be ideal to  have Java make use of the  graphics hardware. A set of Java classes with native  methods defined  would provide the Java program access to these features.</p>
<h4><strong>Sun&#8217;s Java</strong></h4>
<p>In the current implementation from Sun, the Java interpreter is  written in C  and can thus talk to the outside environment as any normal  C program can. A  majority of the Java Runtime is written in Java and  may make calls into the  interpreter or directly to the outside  environment, all via native methods. The  application deals mostly with  the Java Runtime, but it may also talk to the  outside environment via  native methods. For example in the class  <tt>java.lang.Thread</tt> the <tt>setPriority()</tt> method is implemented in  Java but calls the method <tt>setPriority0()</tt>,  which is a native method in  the <tt>Thread</tt> class. This native  method is implemented in C and resides  within the Java virtual machine  itself. On the Windows 95 platform this native  method will then call  (eventually) the Win32 <tt>SetPriority()</tt> API. This is  an example  where the native method implementation was provided by the Java  virtual  machine directly. In most cases the native method implementation  resides  in an external dynamic link library (discussed in a following  section), but the  call still goes through the Java virtual machine.</p>
<h3><strong>Performance</strong></h3>
<p>Another major reason for native methods is performance. The Java  language  trades some performance for features like its dynamic nature,  garbage  collecting, and safety. Some Java implementations, like the  current crop, may be  interpreters, which also add extra overhead. The  lost performance can be small  as the implementation technology for Java  systems improve, but until then and  even after there may always be a  small performance overhead for certain  functionality a Java program may  need. This functionality can be pushed down  into a native method. That  native method can then be implemented efficiently at  the native lower  level of the system on which the Java virtual machine is  running. Once  at the native implementation level, the developer can use the   best-suited language, such as C or even assembler. In this way, maximum   performance can be achieved in those specific areas while the bulk of  the  application is done within the safe and robust Java virtual  machine. One area  where you may choose to implement some parts of an  application in native methods  is time-intensive computations, such as  graphics rendering, simulation models,  and so forth.</p>
<h3><strong>Accessing Existing Libraries</strong></h3>
<p>The fact that Java is targeted at the production of platform-neutral  code  means that the current implementations may not access system  features that you  may need. An example is a database engine. If you  need to, you can use the  native method facility to provide your own  interface to such libraries. Further,  you may want to use Java to write  applications that use existing in-house  libraries. Again, the use of  native methods enables you to make such an  interface. This enables you  to leverage off your existing code base as well as  gradually introduce  Java-based applications among your other applications coded  in an older  language.</p>
<h2><a name="BenefitsandTradeOffs"><strong><span style="color: #ff0000; font-size: large;">Benefits  and  Trade-Offs</span></strong></a></h2>
<p>The presense of native methods offers many benefits, the biggest  being the  extension of Java power. However, there is always a downside  to all good things,  and native methods definitely have their downsides.  Depending on what the goals  of your application are, the downsides may  not be that terrible. Foremost is the  fact that, by definition, the  use of native methods defeats several of Java&#8217;s  main goals: platform  neutrality, system safety, and security.</p>
<p>Some of Java&#8217;s attractive features help minimize the downsides,  however. The  best feature of all is that Java is such a nice language  to develop in you won&#8217;t  want to use native methods unless you have to.</p>
<h3><strong>Platform Neutrality</strong></h3>
<p>Because a native method is implemented in a foreign language, the  platform  neutrality is limited to the language being used. Most likely,  native methods  are implemented in C or C++. Although those languages  have standards, these  standards leave a lot of room for  implementation-defined attributes (even  compilers on the same system  may differ), so your mileage may vary. If the  native method accesses  the underlying system, you are tying your implementation  to that  system. For example, the file systems of UNIX and Win32 have some   differences. There may even be differences between flavors of UNIX and  Win32  (Win95 and WinNT are not identical). Once again, you may  sacrifice your platform  neutrality with your native method. This may  cause you to have to support a  limited number of platforms (rather than  <em>all</em> Java platforms). Further,  for each platform you choose to  support, you may (probably will) have to  implement several flavors of  the native method.</p>
<p>The Java language and runtime provide a number of features that make   applications more robust and safe. Java&#8217;s memory management,  synchronization  features, and lack of address manipulation help prevent  common programming  mistakes from slipping through the development and  testing phases of your  product. However, once you drop out of Java into  a native method, you are, once  again, at the mercy of the language and  system in which you are implementing the  native method. If your native  method implemented in C chooses to manipulate an  address directly, you  risk corrupting some part of memory, perhaps even the Java  virtual  machine itself.</p>
<h3><strong>Security Concerns</strong></h3>
<p>Additionally, the Java Language provides features to aid in the  writing of  secure applications. A Java virtual machine is much more  capable of detecting an  “evil” Java program than an application in  other languages. Once you drop into a  native method, the Java virtual  machine can no longer verify, catch, or prevent  the program from  violating the security of the environment in which the Java  virtual  machine is running. This is the reason a Java-enabled Web browser   typically does not allow a nontrusted native method to be called. In  today&#8217;s  browsers, a trusted native method must be present on the local  system in a  certain location to be executed from an <em>arriving</em> applet (in other words,  one loaded from a remote site). For more  information on security, in general,  see Part 6, “Security.” For more  information on how security applies to native  methods, see <a href="http://www.80x86.cn/ch33.htm">Chapter 33</a>, “Securing Your  Native Method  Libraries.”</p>
<h3><strong>System Safety</strong></h3>
<p>Another potential hazard is the fact that a native method is not  isolated.  When a native method is entered, it not only accesses the  environment outside  the Java virtual machine, it also freely accesses  the Java virtual machine  directly. This is a necessary evil. It gives  the native method quite a bit of  power and flexibility, because it may  need access to information kept within the  virtual machine to do its  job. This flexibility, however, exposes the internals  of the Java  virtual machine to the native method.</p>
<h3><strong>Dependence on the Java Implementation</strong></h3>
<p>It should be obvious that the implementation of native methods is  also  dependent on the Java implementation itself. This means that the  native methods  you write today for use with the Sun implementation of  Java may not work with a  Java implementation from another vendor.</p>
<p>The interface used for the Java virtual machine to call out to native  methods  and the interface that native methods use to access the  internal functions and  data structures of the Java virtual machine are  not, currently, defined by  either the Java Language Specification or  the Java Virtual Machine  Specification. A lot of native methods call  back into the Java virtual machine  for instantiating new objects,  calling Java methods, throwing Java Exceptions,  and so forth. Further,  the method used to lay out Java types is also not  defined. So, although  your native method of today knows how to access the fields  of an  object, this could be different on the Java virtual machine of tomorrow.   This oversight can be greatly helped if a standard API is defined for  both how a  Java program interacts with a native method and how a native  method accesses  data within the Java virtual machine. Even after such  an API,  implementation-defined behavior will likely still be present.</p>
<h3><strong>Java to the Rescue!</strong></h3>
<p>Recall that Java helps to minimize the damage of native methods. When  you  find yourself in the position that you must use native methods,  you can take  advantage of Java&#8217;s features to help isolate the usage and  perhaps maintain a  fair amount of Java&#8217;s advantages.</p>
<h4><strong>The Java Class System</strong></h4>
<p>Because Java narrows the use of native facilities to within the  confines of a  method, it does not affect the design of the program. A  program is still a  collection of classes and all classes still  communicate with each other via  their defined interface-that is, the  classes&#8217; methods. Thus the callers of  native methods do not know they  are calling native methods. Because methods are  discrete operations on  the data of a specific object, they tend to be small  chunks of code.  This implies that native methods tend to be conceptually small,  easily  managed, pieces of code.</p>
<h4><strong>Java Still Works for You</strong></h4>
<p>Java will still perform a variety of duties-such as parameter  checking, stack  checking, synchronization, and so forth-before entering  the actual native code.  It greatly aids the developer in writing  correct native methods. A native method  is capable of creating new  objects and calling Java methods, and it can even  cause exceptions to  be thrown. In the current implementation from Sun, an  exception can be  created by a native method and registered for throwing. When  Java  virtual machine gains control back from the native method, usually  because  of that method returning, the exception will then be thrown.</p>
<p>It&#8217;s a good idea to make your native methods as small as possible and  have  them do a specific task. Do the work that needs to be done and  pass the  information back into the Java method. It&#8217;s also wise to have  your Java classes  make the native methods private, then provide a  public Java method that will  call the private native method. This  enables the Java method to perform error  checks and other data  manipulations, freeing your actual native method  implementation to  focus on its simple task.</p>
<h2><a name="HowDoesThisMagicWork"><strong><span style="color: #ff0000; font-size: large;">How  Does This  Magic Work?</span></strong></a></h2>
<p>Much of the magic of making native methods work is provided in the  next three  chapters. This section provides an introduction, which  neglects many of the  details but should give you a good frame of  reference for understanding the  following chapters. If you don&#8217;t really  want to use native methods, but just  want a basic understanding this  discussion should satisfy your needs.</p>
<h3><strong>Sun&#8217;s Implementation</strong></h3>
<p>Due to the lack of a well-defined interface between a Java  implementation and  its surrounding environment, the details of writing  native methods will most  likely be specific to the implementation of  the Java system you are using. The  next sections are based on the  implementation provided by Sun on the  Solaris-Sparc and Win32-Intel  platforms.</p>
<h4><strong>Using Dynamic Linking</strong></h4>
<p>Sun&#8217;s Java implementation interfaces to native methods by using the  dynamic  linking capabilities of the underlying operation system. The  Java virtual  machine is a complete program, which is already compiled  for its respective  platform. The nature of Java enables it easily to  absorb a Java class and  execute its behavior. However, for a compiled  native method, things are not so  simple. Somehow, the Java virtual  machine must be taught how to call this native  method. This is done by  relying on the implementation of native methods to  reside in a dynamic  link library, which the operating system magically loads and  links into  the process that is running the Java virtual machine. On the Solaris   platform, such a library is often called <em>shared objects</em>, or <em>shared   libraries</em>, or simply <em>dot-so&#8217;s (.so&#8217;s)</em>. On Win32 platforms,  they are  called <em>dynamic link libraries</em> <em>(DLLs)</em>. This  chapter uses DLL to  refer to both.</p>
<p>Both Solaris and Win32 provide the necessary capabilities to achieve  this  dynamic linking. The dynamic linking facilities of both Solaris  and Win32 are  similar in concept, but differ in their details. This  chapter does not attempt  to describe the two in detail; however, if you  wish to do native method  programming, you should understand the  mechanism used by your platform. On  Solaris, you can begin by viewing  the manual page on the <tt>dlopen()</tt> system call, and its  relatives. On Win32, start with the help file on  <tt>LoadLibrary()</tt> and its relatives. Further, you should understand the  calling  conventions and linking convention used by your compiler.</p>
<p>Sometime before a native method is invoked, the Java virtual machine  must be  told to find, load, and link the necessary DLLs, which contain  the native method  implementations. This is conveniently achieved by  using the static method  <tt>java.lang.System.loadLibrary( "mystuff" )</tt>.  It is worth noting here that  the name passed is not the actual  filename of the DLL. Java maps the passed name  into an expected  filename, appropriate for the underlying system, of the DLL. In  the  call described previously, the string <tt>"mystuff"</tt> is mapped to a  DLL  named libmystuff.so on Solaris and mystuff.dll on Win32. If you run  the Java  program under a debugger, Java conveniently maps the same  name  <tt>"mystuff"</tt> to <tt>libmystuff_g.so</tt> and <tt>mystuff_g.dll</tt>.  This  enables you to supply two versions of the DLL-one with debug  symbols, one  without. Java magically finds the right one, dependent on  whether you run under  a debugger or not.</p>
<h4><strong>Defining the Calling Convention</strong></h4>
<p>In, essence, Sun defines the method its Java virtual machine will use  to call  external functions. In order to dynamically link and call the  implementation of  a native method successfully, the Java virtual  machine must know several  details. It must know the name of the  function within the DLL (the  implementation of the native method) to  locate the symbol and its entry point.  It also must know how to call  that function (its return type, number of  parameters, and types of  parameters). The Java virtual machine expects the  functions to be coded  in C using the calling conventions appropriate for the  underlying  architecture (and compiler).</p>
<p>In simple terms, this means the actual function calls Java makes into  the DLL  must be known names; if you are trying to get Java to call  into your existing  library, unless your functions magically match the  names Java expects  (unlikely), you will usually have glue code, which  sits between Java and your  real functions. Java will call the glue  functions<em>,</em> which in turn call in  your functions. Alternatively,  you can modify your functions to use the names  and parameters Java  expects, thus eliminating this extra call; however, in  practice this is  not always feasible, especially when calling existing  libraries.  Figure 30.2 shows the most likely scenarios of how your code will be   segmented.</p>
<p><a href="http://www.80x86.cn/f30-2.gif" class="highslide-image" onclick="return hs.expand(this);"><strong>Figure 30.2 : </strong><em>Java&#8217;s  use of Dynamic Link  Libraries.</em></a></p>
<p>The Sun JDK provides a tool, named <tt>javah</tt>, to help you create  your  native method implementation functions. The developer of native  methods runs  <tt>javah</tt>, passing it the name of a class. <tt>javah</tt> emits both a  header file (.h) and a code file (.c) containing  information about each native  method and relevant type declarations.  The .h file will contain the prototypes  of the functions Java will  call, and thus expect to find in the DLL. The .c file  will contain  stubs for each function. Thus, the developer needs to fill in only  the  details of the functions in the c file and build the DLL appropriately.</p>
<h4><strong>How the Virtual Machine Makes It Work</strong></h4>
<p>When a class is first used by Java, its class descriptor is loaded  into  memory. The <em>class descriptor</em> can be thought of as a  directory for all  services provided by the class-there is only one  class descriptor loaded,  regardless of how many instances of that class  exist. Among its entries is a  list of <em>method descriptors,</em> which  contain information specific to  methods, including where the code is,  what parameters they take, and method  modifiers.</p>
<p>If a method descriptor has its native modifier set, the block will  include a  pointer to the function that implements that native method.  This function  resides in some DLL but will be loaded into the Java  processes address space by  the operating system. At the time the class  descriptor with native methods is  loaded, the associated DLL does not  have to be loaded, and thus the function  pointer will not be set.  Sometime prior to a native method being called, the  associated DLL  should be loaded. This is done via a call to  <tt>java.system.loadLibrary()</tt>.  When this call is made, Java will find and  load the DLL but will still  not resolve symbols; the resolution phase is delayed  until the point  of use. At the time of a call to a native method, Java will  first check  to see whether the native method implementation function has already   been resolved-that is, its pointer is not <tt>null</tt>. If it has been   previously resolved, the call is performed; otherwise, the resolution  of the  symbols is attempted. The resolution is performed by making an  operating system  call to see whether the symbol exists in the caller&#8217;s  address space. This  includes the Java process and any DLLs loaded on  its behalf. On Win32, this is  done via a <tt>GetProcAddress()</tt> and  on Solaris via a <tt>dlsym()</tt> call.</p>
<p>If the symbols are correctly resolved, the call is performed as if  the Java  virtual machine was making a standard C call to its own  internal functions. If  the resolution fails, the exception <tt>java.lang.UnsatisfiedLinkError</tt> will  be thrown at the point of the native method call.</p>
<h2><a name="Summary"><strong><span style="color: #ff0000; font-size: large;">Summary</span></strong></a></h2>
<p>You should now have a basic understanding of how native methods  enable a Java  program to access the outside environment. Whether that  consists of an operating  system, a browser, or your own existing  libraries, your Java code can reach  them. It should now be clear that  native methods do not come without some cost.  You lose a lot of the  benefits of the Java language. When there is no choice,  however, native  methods are there to be used. With the basic understanding of  how  native methods work you should be ready to tackle the next chapters,  which  provide more in-depth examples of native methods in action, as  well as more tips  and tricks to help you.</p>
<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="5"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="优化java代码效率" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F06%2Foptimize-the-efficiency-of-java-code%2F&from=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava-native-method%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">优化java代码效率</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="JavaScript和Java的区别" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2009%2F09%2Fjavascript_compareto_java%2F&from=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava-native-method%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">JavaScript和Java的区别</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="java中关于时间日期操作的常用函数" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2009%2F12%2Fjava-date-operation-function%2F&from=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava-native-method%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">java中关于时间日期操作的常用函数</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Java Enumeration" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F03%2Fjava-enumeration%2F&from=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava-native-method%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Java Enumeration</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Java StringUtil" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava-stringutil%2F&from=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava-native-method%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Java StringUtil</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="5" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>Here is no comments yet by the time  your rss reader get this, Do you want to be the first commentor? Hurry up ]]></content:encoded>
			<wfw:commentRss>http://www.javachen.com/2010/01/java-native-method/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- This Quick Cache file was built for (  www.javachen.com/tag/native/feed/ ) in 4.28008 seconds, on Feb 6th, 2012 at 2:44 am UTC. -->
<!-- This Quick Cache file will automatically expire ( and be re-built automatically ) on Feb 6th, 2012 at 3:44 am UTC -->
