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 9.3 KiB

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