You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

javah.html 10 KiB

9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <!--
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. -->
  15. <html>
  16. <head>
  17. <meta http-equiv="Content-Language" content="en-us">
  18. <link rel="stylesheet" type="text/css" href="../stylesheets/style.css">
  19. <title>Javah Task</title>
  20. </head>
  21. <body>
  22. <h2><a name="javah">Javah</a></h2>
  23. <h3>Description</h3>
  24. <p>Generates JNI headers from a Java class.</p>
  25. <p> When this task executes, it will generate the C header and source files that
  26. are needed to implement native methods. JNI operates differently depending on
  27. whether <a href="http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javah.html">JDK1.2+</a>
  28. or <a href="http://java.sun.com/products/jdk/1.1/docs/tooldocs/win32/javah.html">pre-JDK1.2</a>
  29. systems are used.</p>
  30. <p>If you are building with Java 8 or above consider
  31. using <a href="javac.html"><code>javac</code></a>'s <code>nativeheaderdir</code>
  32. attribute instead which allows you to compile the classes and
  33. generate the native header files with a single step.</p>
  34. <p><b>Note</b> the <code>javah</code> has been deprecated as of Java 9
  35. and removed as of Java 10. Trying to use it with Java10 will
  36. fail.</p>
  37. <p>It is possible to use different compilers. This can be selected
  38. with the <code>implementation</code> attribute or a nested element. <a
  39. name="implementationvalues">Here are the choices of the attribute</a>:</p>
  40. <ul>
  41. <li>default - the default compiler for the platform.</li>
  42. <li>sun (the standard compiler of the JDK) - default when not
  43. running on Kaffee or gcj/gij or Java9.</li>
  44. <li>kaffeh (the native standard compiler of <a href="http://www.kaffe.org" target="_top">Kaffe</a>)</li>
  45. <li>gcjh (the native standard compiler
  46. of <a href="http://gcc.gnu.org/java/"
  47. target="_top">gcj and gij</a>) <em>since Apache Ant 1.8.2</em></li>
  48. <li>forking - runs the javah executable via its command line
  49. interface in a separate process. Default when running on
  50. Java9. <em>since Ant 1.9.8</em></li>
  51. </ul>
  52. <p><b>Note:</b> if you are using this task to work on multiple files
  53. the command line may become too long on some operating systems.
  54. Unfortunately the javah command doesn't support command argument
  55. files the way javac (for example) does, so all that can be done is
  56. breaking the amount of classes to compile into smaller chunks.</p>
  57. <h3>Parameters</h3>
  58. <table border="1" cellpadding="2" cellspacing="0">
  59. <tr>
  60. <td valign="top"><b>Attribute</b></td>
  61. <td valign="top"><b>Description</b></td>
  62. <td valign="top" align="center"><b>Required</b></td>
  63. </tr>
  64. <tr>
  65. <td valign="top">class</td>
  66. <td valign="top">the fully-qualified name of the class (or classes,
  67. separated by commas)</td>
  68. <td align="center" valign="top">Yes</td>
  69. </tr>
  70. <tr>
  71. <td valign="top">outputFile</td>
  72. <td valign="top">concatenates the resulting header or source files for all the classes listed into this file</td>
  73. <td align="center" valign="middle" rowspan="2">Yes</td>
  74. </tr>
  75. <tr>
  76. <td valign="top">destdir</td>
  77. <td valign="top">sets the directory where javah saves the header files or the
  78. stub files.</td>
  79. </tr>
  80. <tr>
  81. <td valign="top">force</td>
  82. <td valign="top">specifies that output files should always be written (JDK1.2 only)</td>
  83. <td valign="top" align="center">No</td>
  84. </tr>
  85. <tr>
  86. <td valign="top">old</td>
  87. <td valign="top">specifies that old JDK1.0-style header files should be generated
  88. (otherwise output file contain JNI-style native method function prototypes) (JDK1.2 only)</td>
  89. <td valign="top" align="center">No</td>
  90. </tr>
  91. <tr>
  92. <td valign="top">stubs</td>
  93. <td valign="top">generate C declarations from the Java object file (used with old)</td>
  94. <td valign="top" align="center">No</td>
  95. </tr>
  96. <tr>
  97. <td valign="top">verbose</td>
  98. <td valign="top">causes Javah to print a message concerning the status of the generated files</td>
  99. <td valign="top" align="center">No</td>
  100. </tr>
  101. <tr>
  102. <td valign="top">classpath</td>
  103. <td valign="top">the classpath to use.</td>
  104. <td align="center" valign="top">No</td>
  105. </tr>
  106. <tr>
  107. <td valign="top">bootclasspath</td>
  108. <td valign="top">location of bootstrap class files.</td>
  109. <td valign="top" align="center">No</td>
  110. </tr>
  111. <tr>
  112. <td valign="top">extdirs</td>
  113. <td valign="top"> location of installed extensions.</td>
  114. <td valign="top" align="center">No</td>
  115. </tr>
  116. <tr>
  117. <td valign="top">implementation</td>
  118. <td valign="top">The compiler implementation to use. If this
  119. attribute is not set, the default compiler for the current VM
  120. will be used. (See the above <a
  121. href="#implementationvalues">list</a> of valid compilers.)</td>
  122. <td align="center" valign="top">No</td>
  123. </tr>
  124. </table>
  125. <p>Either outputFile or destdir must be supplied, but not both.&nbsp;</p>
  126. <h3>Parameters specified as nested elements</h3>
  127. <h4>arg</h4>
  128. <p>You can specify additional command line arguments for the compiler
  129. with nested <code>&lt;arg&gt;</code> elements. These elements are
  130. specified like <a href="../using.html#arg">Command-line Arguments</a>
  131. but have an additional attribute that can be used to enable arguments
  132. only if a given compiler implementation will be used.</p>
  133. <table border="1" cellpadding="2" cellspacing="0">
  134. <tr>
  135. <td width="12%" valign="top"><b>Attribute</b></td>
  136. <td width="78%" valign="top"><b>Description</b></td>
  137. <td width="10%" valign="top"><b>Required</b></td>
  138. </tr>
  139. <tr>
  140. <td valign="top">value</td>
  141. <td align="center" rowspan="4">See
  142. <a href="../using.html#arg">Command-line Arguments</a>.</td>
  143. <td align="center" rowspan="4">Exactly one of these.</td>
  144. </tr>
  145. <tr>
  146. <td valign="top">line</td>
  147. </tr>
  148. <tr>
  149. <td valign="top">file</td>
  150. </tr>
  151. <tr>
  152. <td valign="top">path</td>
  153. </tr>
  154. <tr>
  155. <td valign="top">prefix</td>
  156. <td align="center" rowspan="2">See
  157. <a href="../using.html#arg">Command-line Arguments</a>.
  158. <em>Since Ant 1.8.</em></td>
  159. <td valign="top" align="center">No</td>
  160. </tr>
  161. <tr>
  162. <td valign="top">suffix</td>
  163. <td valign="top" align="center">No</td>
  164. </tr>
  165. <tr>
  166. <td valign="top">implementation</td>
  167. <td>Only pass the specified argument if the chosen compiler
  168. implementation matches the value of this attribute. Legal values
  169. are the same as those in the above <a
  170. href="#implementationvalues">list</a> of valid compilers.)</td>
  171. <td align="center">No</td>
  172. </tr>
  173. </table>
  174. <h4>implementationclasspath <em>since Ant 1.8.0</em></h4>
  175. <p>A <a href="../using.html#path">PATH like structure</a> holding the
  176. classpath to use when loading the compiler implementation if a
  177. custom class has been specified. Doesn't have any effect when
  178. using one of the built-in compilers.</p>
  179. <h4>Any nested element of a type that implements JavahAdapter
  180. <em>since Ant 1.8.0</em></h4>
  181. <p>If a defined type implements the <code>JavahAdapter</code>
  182. interface a nested element of that type can be used as an
  183. alternative to the <code>implementation</code> attribute.</p>
  184. <h3>Examples</h3>
  185. <pre> &lt;javah destdir=&quot;c&quot; class=&quot;org.foo.bar.Wibble&quot;/&gt;</pre>
  186. <p>makes a JNI header of the named class, using the JDK1.2 JNI model. Assuming
  187. the directory 'c' already exists, the file <tt>org_foo_bar_Wibble.h</tt>
  188. is created there. If this file already exists, it is left unchanged.</p>
  189. <pre> &lt;javah outputFile=&quot;wibble.h&quot;&gt;
  190. &lt;class name=&quot;org.foo.bar.Wibble,org.foo.bar.Bobble&quot;/&gt;
  191. &lt;/javah&gt;</pre>
  192. <p>is similar to the previous example, except the output is written to a file
  193. called <tt>wibble.h</tt>
  194. in the current directory.</p>
  195. <pre> &lt;javah destdir=&quot;c&quot; force=&quot;yes&quot;&gt;
  196. &lt;class name=&quot;org.foo.bar.Wibble&quot;/&gt;
  197. &lt;class name=&quot;org.foo.bar.Bobble&quot;/&gt;
  198. &lt;class name=&quot;org.foo.bar.Tribble&quot;/&gt;
  199. &lt;/javah&gt;</pre>
  200. <p>writes three header files, one for each of the classes named. Because the
  201. force option is set, these header files are always written when the Javah task
  202. is invoked, even if they already exist.</p>
  203. <pre> &lt;javah destdir=&quot;c&quot; verbose=&quot;yes&quot; old=&quot;yes&quot; force=&quot;yes&quot;&gt;
  204. &lt;class name=&quot;org.foo.bar.Wibble&quot;/&gt;
  205. &lt;class name=&quot;org.foo.bar.Bobble&quot;/&gt;
  206. &lt;class name=&quot;org.foo.bar.Tribble&quot;/&gt;
  207. &lt;/javah&gt;
  208. &lt;javah destdir=&quot;c&quot; verbose=&quot;yes&quot; stubs=&quot;yes&quot; old=&quot;yes&quot; force=&quot;yes&quot;&gt;
  209. &lt;class name=&quot;org.foo.bar.Wibble&quot;/&gt;
  210. &lt;class name=&quot;org.foo.bar.Bobble&quot;/&gt;
  211. &lt;class name=&quot;org.foo.bar.Tribble&quot;/&gt;
  212. &lt;/javah&gt;</pre>
  213. <p>writes the headers for the three classes using the 'old' JNI format, then
  214. writes the corresponding .c stubs. The verbose option will cause Javah to
  215. describe its progress.</p>
  216. <p>If you want to use a custom
  217. JavahAdapter <code>org.example.MyAdapter</code> you can either
  218. use the implementation attribute:</p>
  219. <pre>
  220. &lt;javah destdir="c" class="org.foo.bar.Wibble"
  221. implementation="org.example.MyAdapter"/&gt;
  222. </pre>
  223. <p>or a define a type and nest this into the task like in:</p>
  224. <pre>
  225. &lt;componentdef classname="org.example.MyAdapter"
  226. name="myadapter"/&gt;
  227. &lt;javah destdir="c" class="org.foo.bar.Wibble"&gt;
  228. &lt;myadapter/&gt;
  229. &lt;/javah&gt;
  230. </pre>
  231. <p>in which case your javah adapter can support attributes and
  232. nested elements of its own.</p>
  233. </body>
  234. </html>