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.

javac.html 35 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  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>Javac Task</title>
  20. </head>
  21. <body>
  22. <h2 id="javac">Javac</h2>
  23. <h3>Description</h3>
  24. <p>Compiles a Java source tree.</p>
  25. <p>The source and destination directory will be recursively scanned for Java source files to
  26. compile. Only <code>.java</code> files that have no corresponding <samp>.class</samp> file or where
  27. the <code>.class</code> file is older than the <samp>.java</samp> file will be compiled.</p>
  28. <p><strong>Note</strong>: Apache Ant uses only the names of the source and class files to find the
  29. classes that need a rebuild. It will not scan the source and therefore will have no knowledge about
  30. nested classes, classes that are named differently from the source file, and so on. See
  31. the <a href="../Tasks/depend.html"><code>&lt;depend&gt;</code></a> task for dependency checking
  32. based on other than just existence/modification times.</p>
  33. <p>When the source files are part of a package, the directory structure of the source tree should
  34. follow the package hierarchy.</p>
  35. <p>It is possible to refine the set of files that are being compiled. This can be done with
  36. the <var>includes</var>, <var>includesfile</var>, <var>excludes</var>, and <var>excludesfile</var>
  37. attributes. With the <var>includes</var> or <var>includesfile</var> attribute, you specify the files
  38. you want to have included. The <var>exclude</var> or <var>excludesfile</var> attribute is used to
  39. specify the files you want to have excluded. In both cases, the list of files can be specified by
  40. either the filename, relative to the directory(s) specified in the <var>srcdir</var> attribute or
  41. nested <code>&lt;src&gt;</code> element(s), or by using wildcard patterns. See the section
  42. on <a href="../dirtasks.html#directorybasedtasks">directory-based tasks</a>, for information on how
  43. the inclusion/exclusion of files works, and how to write wildcard patterns.</p>
  44. <p id="compilervalues">It is possible to use different compilers. This can be specified by either
  45. setting the global <code>build.compiler</code> property, which will affect
  46. all <code>&lt;javac&gt;</code> tasks throughout the build, by setting the <var>compiler</var>
  47. attribute, specific to the current <code>&lt;javac&gt;</code> task or by using a nested element of
  48. any <a href="typedef.html">typedef</a>fed or <a href="componentdef.html">componentdef</a>fed type
  49. that implements <code>org.apache.tools.ant.taskdefs.compilers.CompilerAdapter</code>. Valid values
  50. for either the <code>build.compiler</code> property or the <var>compiler</var> attribute are:</p>
  51. <ul>
  52. <li><q>classic</q> (the standard compiler of JDK 1.1/1.2) &ndash; <q>javac1.1</q>
  53. and <q>javac1.2</q> can be used as aliases.</li>
  54. <li><q>modern</q> (the standard compiler of JDK 1.3 and later)
  55. &ndash; <q>javac1.3</q>, <q>javac1.4</q>, <q>javac1.5</q> and <q>javac1.6</q>, <q>javac1.7</q>
  56. (<em>since Ant 1.8.2</em>), <q>javac1.8</q> (<em>since Ant 1.8.3</em>), <q>javac1.9</q>
  57. (<em>since Ant 1.9.5</em>), <q>javac9</q> (<em>since Ant 1.9.8</em>) and <q>javac10+</q>
  58. (<em>since Ant 1.10.2</em>) can be used as aliases.</li>
  59. <li><q>jikes</q> (the <a href="http://jikes.sourceforge.net/" target="_top">Jikes</a>
  60. compiler).</li>
  61. <li><q>jvc</q> (the Command-Line Compiler from Microsoft's SDK for Java / Visual J++)
  62. &ndash; <q>microsoft</q> can be used as an alias.</li>
  63. <li><q>kjc</q>
  64. (the <a href="https://web.archive.org/web/20050212025507/http://www.dms.at/kopi/index.html"
  65. target="_top">kopi</a> compiler).</li>
  66. <li><q>gcj</q> (the <code>gcj</code> compiler
  67. from <a href="https://gcc.gnu.org/gcc-7/changes.html#java" target="_top">GCC</a>).</li>
  68. <li><q>sj</q> (Symantec Java compiler) &ndash; <q>symantec</q> can be used as an alias.</li>
  69. <li><q>extJavac</q> (run either modern or classic in a JVM of its own).</li>
  70. </ul>
  71. <p>The default is <q>javac1.x</q> with <q>x</q> depending on the JDK version you use while you are
  72. running Ant. If you wish to use a different compiler interface than those supplied, you can write a
  73. class that implements the CompilerAdapter interface
  74. (package <code>org.apache.tools.ant.taskdefs.compilers</code>). Supply the full classname in
  75. the <code>build.compiler</code> property or the <var>compiler</var> attribute.</p>
  76. <p>The <var>fork</var> attribute overrides the <code>build.compiler</code> property
  77. or <var>compiler</var> attribute setting and expects a JDK 1.1 or higher to be set
  78. in <code>JAVA_HOME</code>.</p>
  79. <p>You can also use the <var>compiler</var> attribute to tell Ant which JDK version it shall assume
  80. when it puts together the command line switches&mdash;even if you set <var>fork</var>=<q>true</q>.
  81. This is useful if you want to run the compiler of JDK 1.1 while your current JDK is 1.2+. If you
  82. use <var>compiler</var>=<q>javac1.1</q> and (for example) <var>depend</var>=<q>true</q>, Ant will
  83. use the command line switch <kbd>-depend</kbd> instead of <kbd>-Xdepend</kbd>.</p>
  84. <p>This task will drop all entries that point to non-existent files/directories from the classpath
  85. it passes to the compiler.</p>
  86. <p>The working directory for a forked executable (if any) is the project's base directory.</p>
  87. <p><strong>Windows Note</strong>: When the modern compiler is used in unforked mode on Windows, it
  88. locks up the files present in the classpath of the <code>&lt;javac&gt;</code> task, and does not
  89. release them. The side effect of this is that you will not be able to delete or move those files
  90. later on in the build. The workaround is to fork when invoking the compiler.</p>
  91. <p>If you are using Java 8 or above and your source contains native methods or fields annotated with
  92. the <code>@Native</code> annotation you can set the <var>nativeheaderdir</var> attribute in order to
  93. use the <kbd>-h</kbd> switch of <kbd>javac</kbd> to generate the native header files. Note that
  94. the logic Ant uses to determine which files to compile does not take native headers into account,
  95. i.e. if the <samp>.class</samp> is more recent than the corresponding <samp>.java</samp> file the
  96. file will not get compiled even if a native header file generated for it would be outdated.</p>
  97. <h3>Parameters</h3>
  98. <table class="attr">
  99. <tr>
  100. <th>Attribute</th>
  101. <th>Description</th>
  102. <th>Required</th>
  103. </tr>
  104. <tr>
  105. <td>srcdir</td>
  106. <td>Location of the java files. (See the <a href="#srcdirnote">note</a> below.)</td>
  107. <td>Yes, unless nested <code>&lt;src&gt;</code> elements or <var>modulesourcepath</var>
  108. attribute or corresponding elements are present</td>
  109. </tr>
  110. <tr>
  111. <td>destdir</td>
  112. <td>Location to store the class files.</td>
  113. <td>No</td>
  114. </tr>
  115. <tr>
  116. <td>includes</td>
  117. <td>Comma- or space-separated list of patterns of files that must be included.</td>
  118. <td>No; defaults to all <samp>.java</samp> files</td>
  119. </tr>
  120. <tr>
  121. <td>includesfile</td>
  122. <td>Name of a file. Each line of this file is taken to be an include pattern.</td>
  123. <td>No</td>
  124. </tr>
  125. <tr>
  126. <td>excludes</td>
  127. <td>Comma- or space-separated list of patterns of files that must be excluded.</td>
  128. <td>No; defaults to default excludes or none if <var>defaultexcludes</var> is <q>no</q></td>
  129. </tr>
  130. <tr>
  131. <td>excludesfile</td>
  132. <td>Name of a file. Each line of this file is taken to be an exclude pattern.</td>
  133. <td>No</td>
  134. </tr>
  135. <tr>
  136. <td>defaultexcludes</td>
  137. <td>Indicates whether default excludes should be used or not (<q>yes|no</q>).</td>
  138. <td>No; defaults to <q>yes</q></td>
  139. </tr>
  140. <tr>
  141. <td>classpath</td>
  142. <td>The classpath to use.</td>
  143. <td>No</td>
  144. </tr>
  145. <tr>
  146. <td>sourcepath</td>
  147. <td>The sourcepath to use. To suppress the <code>sourcepath</code> switch,
  148. use <var>sourcepath</var>=<q></q>.</td>
  149. <td>No; defaults to <var>srcdir</var> unless nested <code>&lt;src&gt;</code> elements are
  150. specified</td>
  151. </tr>
  152. <tr>
  153. <td>bootclasspath</td>
  154. <td>Location of bootstrap class files. (See <a href="#bootstrap">below</a> for using
  155. the <kbd>-X</kbd> and <kbd>-J-X</kbd> parameters for specifying the bootstrap
  156. classpath).</td>
  157. <td>No</td>
  158. </tr>
  159. <tr>
  160. <td>classpathref</td>
  161. <td>The <var>classpath</var> to use, given as a <a href="../using.html#references">reference</a>
  162. to a path defined elsewhere.</td>
  163. <td>No</td>
  164. </tr>
  165. <tr>
  166. <td>sourcepathref</td>
  167. <td>The <var>sourcepath</var> to use, given as
  168. a <a href="../using.html#references">reference</a> to a path defined elsewhere.</td>
  169. <td>No</td>
  170. </tr>
  171. <tr>
  172. <td>bootclasspathref</td>
  173. <td>The <var>bootstrapclasspath</var> to use, given as
  174. a <a href="../using.html#references">reference</a> to a path defined elsewhere.</td>
  175. <td>No</td>
  176. </tr>
  177. <tr>
  178. <td>extdirs</td>
  179. <td>Location of installed extensions.</td>
  180. <td>No</td>
  181. </tr>
  182. <tr>
  183. <td>encoding</td>
  184. <td>Encoding of source files. (<strong>Note</strong>: <code>gcj</code> doesn't support this
  185. option yet.)</td>
  186. <td>No</td>
  187. </tr>
  188. <tr>
  189. <td>nowarn</td>
  190. <td>Indicates whether the <kbd>-nowarn</kbd> switch should be passed to the compiler.</td>
  191. <td>No; defaults to <q>off</q></td>
  192. </tr>
  193. <tr>
  194. <td>debug</td>
  195. <td>Indicates whether source should be compiled with debug information. If set
  196. to <q>off</q>, <kbd>-g:none</kbd> will be passed on the command line for compilers that
  197. support it (for other compilers, no command line argument will be used). If set
  198. to <q>true</q>, the value of the <var>debuglevel</var> attribute determines the command line
  199. argument.</td>
  200. <td>No; defaults to <q>off</q></td>
  201. </tr>
  202. <tr>
  203. <td>debuglevel</td>
  204. <td>Keyword list to be appended to the <kbd>-g</kbd> command-line switch. Legal values
  205. are <q>none</q> or a comma-separated list of the following
  206. keywords: <q>lines</q>, <q>vars</q>, and <q>source</q>.</td>
  207. <td>No; ignored when <var>debug</var> is <q>false</q> or any implementation other
  208. than <q>modern</q>, <q>javac1.2</q> and <q>jikes</q>; by default, nothing will be appended
  209. to <kbd>-g</kbd></td>
  210. </tr>
  211. <tr>
  212. <td>optimize</td>
  213. <td>Indicates whether source should be compiled with optimization. <strong>Note</strong> that
  214. this flag is just ignored by Sun's <kbd>javac</kbd> since JDK 1.3 (because compile-time
  215. optimization is unnecessary).</td>
  216. <td>No; defaults to <q>off</q></td>
  217. </tr>
  218. <tr>
  219. <td>deprecation</td>
  220. <td>Indicates whether source should be compiled with deprecation information.</td>
  221. <td>No; defaults to <code>off</code></td>
  222. </tr>
  223. <tr>
  224. <td>verbose</td>
  225. <td>Asks the compiler for verbose output.</td>
  226. <td>No; defaults to <q>no</q></td>
  227. </tr>
  228. <tr>
  229. <td>depend</td>
  230. <td>Enables dependency tracking for compilers that support this (<q>jikes</q>
  231. and <q>classic</q>).</td>
  232. <td>No</td>
  233. </tr>
  234. <tr>
  235. <td>includeAntRuntime</td>
  236. <td>Whether to include the Ant run-time libraries in the classpath. <em>It is usually best</em>
  237. to set this to <q>false</q> so the script's behavior is not sensitive to the environment in
  238. which it is run.</td>
  239. <td>No; defaults to <q>yes</q>,
  240. unless <a href="../sysclasspath.html"><code>build.sysclasspath</code></a> property is set</td>
  241. </tr>
  242. <tr>
  243. <td>includeJavaRuntime</td>
  244. <td>Whether to include the default run-time libraries from the executing JVM in the
  245. classpath.<br/><strong>Note</strong>: In some setups the run-time libraries may be part of the
  246. "Ant run-time libraries" so you may need to explicitly set <var>includeAntRuntime</var>
  247. to <q>false</q> to ensure that the Java run-time libraries are not included.</td>
  248. <td>No; defaults to <q>no</q></td>
  249. </tr>
  250. <tr>
  251. <td>fork</td>
  252. <td>Whether to execute <code>javac</code> using the JDK compiler externally.</td>
  253. <td>No; defaults to <q>no</q></td>
  254. </tr>
  255. <tr>
  256. <td>executable</td>
  257. <td>Complete path to the <kbd>javac</kbd> executable to use in case of <var>fork</var>
  258. is <q>yes</q>.<br/><em>Since Ant 1.6</em> this attribute can also be used to specify the path
  259. to the executable when using <q>jikes</q>, <q>jvc</q>, <q>gcj</q> or <q>sj</q>.</td>
  260. <td>No; defaults to the compiler of current JDK, ignored if <var>fork</var> is <q>no</q></td>
  261. </tr>
  262. <tr>
  263. <td>memoryInitialSize</td>
  264. <td>The initial size of the memory for the underlying JVM, if <kbd>javac</kbd> is run
  265. externally. (Examples: <q>83886080</q>, <q>81920k</q>, or <q>80m</q>)</td>
  266. <td>No; defaults to the standard JVM memory setting, ignored if <var>fork</var>
  267. is <q>no</q></td>
  268. </tr>
  269. <tr>
  270. <td>memoryMaximumSize</td>
  271. <td>The maximum size of the memory for the underlying JVM, if <kbd>javac</kbd> is run
  272. externally; ignored otherwise. (Examples: <q>83886080</q>, <q>81920k</q>, or <q>80m</q>)</td>
  273. <td>No; defaults to the standard JVM memory setting, ignored if <var>fork</var>
  274. is <q>no</q></td>
  275. </tr>
  276. <tr>
  277. <td>failonerror</td>
  278. <td>Indicates whether compilation errors will fail the build>.</td>
  279. <td>No; defaults to <q>true</q></td>
  280. </tr>
  281. <tr>
  282. <td>errorProperty</td>
  283. <td>The property to set to <q>true</q> if compilation fails. <em>Since Ant 1.7.1</em>.</td>
  284. <td>No</td>
  285. </tr>
  286. <tr>
  287. <td>source</td>
  288. <td>Java language features accepted by compiler, as specified by the <kbd>-source</kbd>
  289. command-line switch. Valid feature versions are <q>1.3</q>, <q>1.4</q>, <q>1.5</q>
  290. or <q>5</q>, etc. The attribute will be ignored by all implementations prior
  291. to <q>javac1.4</q> (or <q>modern</q> when Ant is not running in a JVM 1.3), <q>gcj</q>
  292. and <q>jikes</q>.<br/> If you use this attribute together with <q>gcj</q> or <q>jikes</q>, you
  293. must make sure that your version supports the <kbd>-source</kbd> (or <kbd>-fsource</kbd>
  294. for <kbd>gcj</kbd>) switch.</td>
  295. <td>No; by default, no <kbd>-source</kbd> argument will be used at all unless the magic
  296. <a href="../javacprops.html#source"><code>ant.build.javac.source</code></a> property is
  297. set<br/><strong>Note that the default value depends on JDK that is running Ant. We highly
  298. recommend to always specify this attribute.</strong></td>
  299. </tr>
  300. <tr>
  301. <td>target</td>
  302. <td>Generate class files for specific JVM version (cross-compile).</td>
  303. <td>No; by default, no <kbd>-target</kbd> argument will be used at all unless the
  304. magic <a href="../javacprops.html#target"><code>ant.build.javac.target</code></a> property is
  305. set<br/><strong>Note that the default value depends on JDK that is running Ant and
  306. on <var>source</var>
  307. (see <a href="https://docs.oracle.com/javase/8/docs/technotes/tools/windows/javac.html#BHCJDCID"
  308. target="_top">Cross-Compilation Options</a>). We highly recommend to always specify this
  309. attribute.</strong></td>
  310. </tr>
  311. <tr>
  312. <td>compiler</td>
  313. <td>The compiler implementation to use. See the above <a href="#compilervalues">list</a> of
  314. valid compilers.</td>
  315. <td>No; defaults to the value of the <code>build.compiler</code> property, if set, or default
  316. compiler for the current JDK otherwise</td>
  317. </tr>
  318. <tr>
  319. <td>listfiles</td>
  320. <td>Indicates whether the source files to be compiled will be listed.</td>
  321. <td>No; defaults to <q>no</q></td>
  322. </tr>
  323. <tr>
  324. <td>tempdir</td>
  325. <td>Where Ant should place temporary files. This is only used if the task is forked and the
  326. command line args length exceeds 4 kB. <em>Since Ant 1.6</em>.</td>
  327. <td>No; default is <code>java.io.tmpdir</code></td>
  328. </tr>
  329. <tr>
  330. <td>updatedProperty</td>
  331. <td>The property to set to <q>true</q> if compilation has taken place and has been
  332. successful. <em>Since Ant 1.7.1</em>.</td>
  333. <td>No</td>
  334. </tr>
  335. <tr>
  336. <td>includeDestClasses</td>
  337. <td>This attribute controls whether to include the destination classes directory in the
  338. classpath given to the compiler. If set to <q>true</q> (default), previously compiled classes
  339. are on the classpath for the compiler. This means that "greedy" compilers will not recompile
  340. dependent classes that are already compiled. In general this is a good thing as it stops the
  341. compiler for doing unnecessary work. However, for some edge cases, involving generics,
  342. the <kbd>javac</kbd> compiler needs to compile the dependent classes to get the generics
  343. information. One example is documented in the bug
  344. report: <a href="https://issues.apache.org/bugzilla/show_bug.cgi?id=40776" target="_top">Bug
  345. 40776 - a problem compiling a Java 5 project with generics</a>. Setting the attribute
  346. to <q>false</q> will cause the compiler to recompile dependent classes. <em>Since Ant
  347. 1.7.1</em>.</td>
  348. <td>No; default is <q>true</q></td>
  349. </tr>
  350. <tr>
  351. <td>createMissingPackageInfoClass</td>
  352. <td>Some package level annotations in <samp>package-info.java</samp> files don't create
  353. any <samp>package-info.class</samp> files so Ant would recompile the same file every
  354. time.<br/><em>Since Ant 1.8</em>, an empty <samp>package-info.class</samp> is created for
  355. each <samp>package-info.java</samp> if there isn't one created by the compiler.<br/>In some
  356. setups this additional class causes problems and it can be suppressed by setting this
  357. attribute to <q>false</q>. <em>Since Ant 1.8.3</em>.</td>
  358. <td>No; default is <q>true</q></td>
  359. </tr>
  360. <tr>
  361. <td>modulepath</td>
  362. <td>Specify where to find application modules. A list of directories of modules, module files or
  363. exploded modules. <em>since Ant 1.9.7</em></td>
  364. <td>No</td>
  365. </tr>
  366. <tr>
  367. <td>modulepathref</td>
  368. <td>The <var>modulepath</var> to use, given as <a href="../using.html#references">reference</a>
  369. to a path defined elsewhere. <em>since Ant 1.9.7</em></td>
  370. <td>No</td>
  371. </tr>
  372. <tr>
  373. <td>modulesourcepath</td>
  374. <td>Specify where to find input source files for multiple module compilation. <em>since Ant
  375. 1.9.7</em></td>
  376. <td>Yes, unless <var>srcdir</var> attribute or nested <code>&lt;src&gt;</code> elements are
  377. present</td>
  378. </tr>
  379. <tr>
  380. <td>modulesourcepathref</td>
  381. <td>The <var>modulesourcepath</var> to use, given
  382. as <a href="../using.html#references">reference</a> to a path defined elsewhere. <em>since
  383. Ant 1.9.7</em></td>
  384. <td>No</td>
  385. </tr>
  386. <tr>
  387. <td>upgrademodulepath</td>
  388. <td>Specify the location of modules that replace upgradeable modules in the runtime
  389. image. <em>since Ant 1.9.7</em></td>
  390. <td>No</td>
  391. </tr>
  392. <tr>
  393. <td>upgrademodulepathref</td>
  394. <td>The <var>upgrademodulepath</var> to use, given
  395. as <a href="../using.html#references">reference</a> to a path defined elsewhere. <em>since Ant
  396. 1.9.7</em></td>
  397. <td>No</td>
  398. </tr>
  399. <tr>
  400. <td>nativeheaderdir</td>
  401. <td>Specify where to place generated native header files. <em>Since Ant 1.9.8</em>.
  402. <td>No, ignored when running on JDK 7 or earlier</td>
  403. </tr>
  404. <tr>
  405. <td>release</td>
  406. <td>Specify the value for the <kbd>--release</kbd> switch.<br/>When set and running on JDK 9+
  407. the <var>source</var> and <var>target</var> attributes as well as the <var>bootclasspath</var>
  408. will be ignored. <em>Since Ant 1.9.8</em>.
  409. <td>No, ignored when running on JDK 8 or earlier</td>
  410. </tr>
  411. </table>
  412. <h3>Parameters specified as nested elements</h3>
  413. <p>This task forms an implicit <a href="../Types/fileset.html">FileSet</a> and supports most
  414. attributes of <code>&lt;fileset&gt;</code> (<var>dir</var> becomes <var>srcdir</var>) as well as the
  415. nested <code>&lt;include&gt;</code>, <code>&lt;exclude&gt;</code>
  416. and <code>&lt;patternset&gt;</code> elements.</p>
  417. <h4><code>srcdir</code>, <code>classpath</code>, <code>sourcepath</code>, <code>bootclasspath</code>,
  418. <code>modulepath</code>, <code>modulesourcepath</code>, <code>upgrademodulepath</code> and <code>extdirs</code></h4>
  419. <p><code>&lt;javac&gt;</code>'s <var>srcdir</var>, <var>classpath</var>, <var>sourcepath</var>,
  420. <var>bootclasspath</var>, <var>extdirs</var>, <var>modulepath</var>, <var>modulesourcepath</var>,
  421. and <var>upgrademodulepath</var> attributes are <a href="../using.html#path">path-like
  422. structures</a> and can also be set via nested <code>&lt;src&gt;</code> (note the different
  423. name!), <code>&lt;classpath&gt;</code>, <code>&lt;sourcepath&gt;</code>, <code>&lt;bootclasspath&gt;</code>,
  424. <code>&lt;extdirs&gt;</code>, <code>&lt;modulepath&gt;</code>, <code>&lt;modulesourcepath&gt;</code>
  425. and <code>&lt;upgrademodulepath&gt;</code> elements, respectively.</p>
  426. <h4>compilerarg</h4>
  427. <p>You can specify additional command line arguments for the compiler with
  428. nested <code>&lt;compilerarg&gt;</code> elements. These elements are specified
  429. like <a href="../using.html#arg">Command-line Arguments</a> but have an additional attribute that
  430. can be used to enable arguments only if a given compiler implementation will be used.</p>
  431. <table class="attr">
  432. <tr>
  433. <th>Attribute</th>
  434. <th>Description</th>
  435. <th>Required</th>
  436. </tr>
  437. <tr>
  438. <td>value</td>
  439. <td rowspan="4">See <a href="../using.html#arg">Command-line Arguments</a>.</td>
  440. <td rowspan="4">Exactly one of these</td>
  441. </tr>
  442. <tr>
  443. <td class="var">line</td>
  444. </tr>
  445. <tr>
  446. <td class="var">file</td>
  447. </tr>
  448. <tr>
  449. <td class="var">path</td>
  450. </tr>
  451. <tr>
  452. <td>prefix</td>
  453. <td rowspan="2">See <a href="../using.html#arg">Command-line Arguments</a>. <em>Since Ant
  454. 1.8</em>.</td>
  455. <td>No</td>
  456. </tr>
  457. <tr>
  458. <td>suffix</td>
  459. <td>No</td>
  460. </tr>
  461. <tr>
  462. <td>compiler</td>
  463. <td>Only pass the specified argument if the chosen compiler implementation matches the value of
  464. this attribute. Legal values are the same as those in the
  465. above <a href="#compilervalues">list</a> of valid compilers.)</td>
  466. <td>No</td>
  467. </tr>
  468. </table>
  469. <h4>compilerclasspath</h4>
  470. <p><em>Since Ant 1.8.0</em></p>
  471. <p>A <a href="../using.html#path">path-like structure</a> holding the classpath to use when loading
  472. the compiler implementation if a custom class has been specified. Doesn't have any effect when
  473. using one of the built-in compilers.</p>
  474. <h4>Any nested element of a type that implements <code>CompilerAdapter</code></h4>
  475. <p><em>Since Ant 1.8.0</em></p>
  476. <p>If a defined type implements the <code>CompilerAdapter</code> interface a nested element of that
  477. type can be used as an alternative to the <var>compiler</var> attribute.</p>
  478. <h3>Examples</h3>
  479. <pre>
  480. &lt;javac srcdir=&quot;${src}&quot;
  481. destdir=&quot;${build}&quot;
  482. classpath=&quot;xyz.jar&quot;
  483. debug=&quot;on&quot;
  484. source=&quot;1.4&quot;/&gt;</pre>
  485. <p>compiles all <samp>.java</samp> files under the <samp>${src}</samp> directory, and stores
  486. the <samp>.class</samp> files in the <samp>${build}</samp> directory. The classpath used
  487. includes <samp>xyz.jar</samp>, and compiling with debug information is on. The source level
  488. is <q>1.4</q>, so you can use <code>assert</code> statements.</p>
  489. <pre>
  490. &lt;javac srcdir=&quot;${src}&quot;
  491. destdir=&quot;${build}&quot;
  492. fork=&quot;true&quot;
  493. source=&quot;1.2&quot;
  494. target=&quot;1.2&quot;/&gt;</pre>
  495. <p>compiles all <samp>.java</samp> files under the <samp>${src}</samp> directory, and stores
  496. the <samp>.class</samp> files in the <samp>${build}</samp> directory. This will fork off the Java
  497. compiler using the default <kbd>javac</kbd> executable. The source level is <q>1.2</q> (similar
  498. to <q>1.1</q> or <q>1.3</q>) and the class files should be runnable under JDK 1.2+ as well.</p>
  499. <pre>
  500. &lt;javac srcdir=&quot;${src}&quot;
  501. destdir=&quot;${build}&quot;
  502. fork=&quot;java$$javac.exe&quot;
  503. source=&quot;1.5&quot;/&gt;</pre>
  504. <p>compiles all <samp>.java</samp> files under the <samp>${src}</samp> directory, and stores
  505. the <samp>.class</samp> files in the <samp>${build}</samp> directory. This will fork off the Java
  506. compiler, using the executable named <kbd>java$javac.exe</kbd>. Note that the <q>$</q> sign needs
  507. to be escaped by a second one. The source level is <q>1.5</q>, so you can use generics.</p>
  508. <pre>
  509. &lt;javac srcdir=&quot;${src}&quot;
  510. destdir=&quot;${build}&quot;
  511. includes=&quot;mypackage/p1/**,mypackage/p2/**&quot;
  512. excludes=&quot;mypackage/p1/testpackage/**&quot;
  513. classpath=&quot;xyz.jar&quot;
  514. debug=&quot;on&quot;/&gt;</pre>
  515. <p>compiles <samp>.java</samp> files under the <samp>${src}</samp> directory, and stores
  516. the <samp>.class</samp> files in the <code>${build}</code> directory. The classpath used
  517. includes <samp>xyz.jar</samp>, and debug information is on. Only files
  518. under <samp>mypackage/p1</samp> and <samp>mypackage/p2</samp> are used. All files in and below
  519. the <samp>mypackage/p1/testpackage</samp> directory are excluded from compilation. You didn't
  520. specify a source or target level, so the actual values used will depend on which JDK you ran Ant
  521. with.</p>
  522. <pre>
  523. &lt;javac srcdir=&quot;${src}:${src2}&quot;
  524. destdir=&quot;${build}&quot;
  525. includes=&quot;mypackage/p1/**,mypackage/p2/**&quot;
  526. excludes=&quot;mypackage/p1/testpackage/**&quot;
  527. classpath=&quot;xyz.jar&quot;
  528. debug=&quot;on&quot;/&gt;</pre>
  529. <p>is the same as the previous example, with the addition of a second source path, defined by the
  530. property <samp>src2</samp>. This can also be represented using nested <code>&lt;src&gt;</code>
  531. elements as follows:</p>
  532. <pre>
  533. &lt;javac destdir=&quot;${build}&quot;
  534. classpath=&quot;xyz.jar&quot;
  535. debug=&quot;on&quot;&gt;
  536. &lt;src path=&quot;${src}&quot;/&gt;
  537. &lt;src path=&quot;${src2}&quot;/&gt;
  538. &lt;include name=&quot;mypackage/p1/**&quot;/&gt;
  539. &lt;include name=&quot;mypackage/p2/**&quot;/&gt;
  540. &lt;exclude name=&quot;mypackage/p1/testpackage/**&quot;/&gt;
  541. &lt;/javac&gt;</pre>
  542. <p>If you want to run the <kbd>javac</kbd> compiler of a different JDK, you should tell Ant, where
  543. to find the compiler and which version of JDK you will be using so it can choose the correct command
  544. line switches. The following example executes a JDK 1.1 <kbd>javac</kbd> in a new process and
  545. uses the correct command line switches even when Ant is running in a JVM of a different version:</p>
  546. <pre>
  547. &lt;javac srcdir=&quot;${src}&quot;
  548. destdir=&quot;${build}&quot;
  549. fork=&quot;yes&quot;
  550. executable=&quot;/opt/java/jdk1.1/bin/javac&quot;
  551. compiler=&quot;javac1.1&quot;/&gt;</pre>
  552. <p id="srcdirnote"><strong>Note</strong>: If you wish to compile only source files located in
  553. certain packages below a common root, use the <var>include</var>/<var>exclude</var> attributes
  554. or <code>&lt;include&gt;</code>/<code>&lt;exclude&gt;</code> nested elements to filter for these
  555. packages. Do not include part of your package structure in the <var>srcdir</var> attribute (or
  556. nested <code>&lt;src&gt;</code> elements), or Ant will recompile your source files every time you
  557. run your compile target. See the <a href="https://ant.apache.org/faq.html#always-recompiles"
  558. target="_top">Ant FAQ</a> for additional information.</p>
  559. <p>If you wish to compile only files explicitly specified and disable <code>javac</code>'s default
  560. searching mechanism then you can unset the <var>sourcepath</var> attribute:</p>
  561. <pre>
  562. &lt;javac sourcepath=&quot;&quot; srcdir=&quot;${src}&quot;
  563. destdir=&quot;${build}&quot; &gt;
  564. &lt;include name="**/*.java"/&gt;
  565. &lt;exclude name="**/Example.java"/&gt;
  566. &lt;/javac&gt;</pre>
  567. <p>That way the <code>javac</code> will compile all Java source files under <samp>${src}</samp>
  568. directory but skip the examples. The compiler will even produce errors if some of the non-example
  569. files refers to them.</p>
  570. <p>If you wish to compile with a special JDK (another than the one Ant is currently using), set
  571. the <var>executable</var> and <var>fork</var> attribute. Using <var>taskname</var> could show in the
  572. log, that these settings are fix.</p>
  573. <pre>
  574. &lt;javac srcdir=&quot;&quot;
  575. destdir=&quot;&quot;
  576. executable=&quot;path-to-java14-home/bin/javac&quot;
  577. fork=&quot;true&quot;
  578. taskname=&quot;javac1.4&quot;/&gt;</pre>
  579. <p><strong>Note</strong>: If you are using Ant on Windows and a new DOS window pops up for every use
  580. of an external compiler, this may be a problem of the JDK you are using. This problem may occur
  581. with all JDKs prior to 1.2.</p>
  582. <p>If you want to activate other compiler options like <code>lint</code> you could use
  583. the <code>&lt;compilerarg&gt;</code> element:</p>
  584. <pre>
  585. &lt;javac srcdir="${src.dir}"
  586. destdir="${classes.dir}"
  587. classpathref="libraries"&gt;
  588. &lt;compilerarg value="-Xlint"/&gt;
  589. &lt;/javac&gt;</pre>
  590. <p>If you want to use a custom CompilerAdapter <code>org.example.MyAdapter</code> you can either use
  591. the <var>compiler</var> attribute:</p>
  592. <pre>
  593. &lt;javac srcdir="${src.dir}"
  594. destdir="${classes.dir}"
  595. compiler="org.example.MyAdapter"/&gt;</pre>
  596. <p>or a define a type and nest this into the task like in:</p>
  597. <pre>
  598. &lt;componentdef classname="org.example.MyAdapter"
  599. name="myadapter"/&gt;
  600. &lt;javac srcdir="${src.dir}"
  601. destdir="${classes.dir}"&gt;
  602. &lt;myadapter/&gt;
  603. &lt;/javac&gt;</pre>
  604. <p>in which case your compiler adapter can support attributes and nested elements of its own.</p>
  605. <pre>
  606. &lt;javac srcdir=&quot;${src}&quot;
  607. destdir=&quot;${build}&quot;
  608. includeantruntime=&quot;false&quot;
  609. modulepath=&quot;modules&quot;
  610. source=&quot;9&quot;/&gt;</pre>
  611. <p>compiles all <samp>.java</samp> files in a single module under the <samp>${src}</samp> directory,
  612. and stores the <samp>.class</samp> files in the <code>${build}</code> directory. The compilation
  613. uses application modules located in <samp>modules</samp> folder.The source level is <q>9</q> to
  614. enable modules.</p>
  615. <pre>
  616. &lt;javac modulesourcepath=&quot;${src}/*/{gen,lin{32,64}}/classes&quot;
  617. destdir=&quot;${build}&quot;
  618. includeantruntime=&quot;false&quot;
  619. modulepath=&quot;modules&quot;
  620. source=&quot;9&quot;/&gt;</pre>
  621. <p>compiles all <samp>.java</samp> files in <samp>gen/classes</samp>, <samp>lin32/classes</samp>
  622. and <code>lin64/classes</code> in all source modules under the <code>${src}</code> directory.
  623. Generates module directories in the <samp>${build}</samp> directory. Each generated module directory
  624. under the <samp>${build}</samp> directory contains <code>.class</code> files from corresponding
  625. source module. The <samp>*</samp> is a token representing the name of any of the modules in the
  626. compilation module set. The <code>{ ... , ... }</code> express alternates for expansion. The
  627. compilation uses application modules located in <code>modules</code> folder.The source level
  628. is <q>9</q> to enable modules.</p>
  629. <h3 id="jikes">Jikes notes</h3>
  630. <p>You need Jikes 1.15 or later.</p>
  631. <p>Jikes supports some extra options, which can be set be defining the properties shown below prior
  632. to invoking the task. The setting for each property will be in effect for
  633. all <code>&lt;javac&gt;</code> tasks throughout the build. The Ant developers are aware that this
  634. is ugly and inflexible &ndash; expect a better solution in the future. All the options are boolean,
  635. and must be set to <q>true</q> or <q>yes</q> to be interpreted as anything other
  636. than <q>false</q>. By default, <code>build.compiler.warnings</code> is <q>true</q>, while all others
  637. are <q>false</q>.</p>
  638. <table>
  639. <tr>
  640. <th>Property</th>
  641. <th>Description</th>
  642. <th>Default</th>
  643. </tr>
  644. <tr>
  645. <td><code>build.compiler.emacs</code></td>
  646. <td>Enable emacs-compatible error messages.</td>
  647. <td><q>false</q></td>
  648. </tr>
  649. <tr>
  650. <td><code>build.compiler.fulldepend</code></td>
  651. <td>Enable full dependency checking; see<br/> the <code>+F</code> switch in the Jikes
  652. manual.</td>
  653. <td><q>false</q></td>
  654. </tr>
  655. <tr>
  656. <td><code>build.compiler.pedantic</code></td>
  657. <td>Enable pedantic warnings.</td>
  658. <td><q>false</q></td>
  659. </tr>
  660. <tr>
  661. <td><code>build.compiler.warnings</code><br/><em><u>Deprecated</u></em>.
  662. Use <code>&lt;javac&gt;</code>'s <var>nowarn</var> attribute instead.</td>
  663. <td>Don't disable warning messages.</td>
  664. <td><q>true</q></td>
  665. </tr>
  666. </table>
  667. <h3 id="jvc">Jvc notes</h3>
  668. <p><q>Jvc</q> will enable Microsoft extensions unless you set the
  669. property <code>build.compiler.jvc.extensions</code> to <q>false</q> before
  670. invoking <code>&lt;javac&gt;</code>.</p>
  671. <h3 id="bootstrap">Bootstrap options</h3>
  672. <p>The Sun <kbd>javac</kbd> compiler has a <kbd>-bootclasspath</kbd> command line
  673. option&mdash;this corresponds to the <var>bootclasspath</var> attribute/element of
  674. the <code>&lt;javac&gt;</code> task. The Sun compiler also allows more control over the boot
  675. classpath using the <kbd>-X</kbd> and <kbd>-J-X</kbd> attributes. One can set these by using
  676. the <code>&lt;compilerarg&gt;</code>. <em>Since Ant 1.6.0</em>, there is a shortcut to convert path
  677. references to strings that can by used in an OS independent fashion
  678. (see <a href="../using.html#pathshortcut">pathshortcut</a>). For example:</p>
  679. <pre>
  680. &lt;path id="lib.path.ref"&gt;
  681. &lt;fileset dir="lib" includes="*.jar"/&gt;
  682. &lt;/path&gt;
  683. &lt;javac srcdir="src" destdir="classes"&gt;
  684. &lt;compilerarg arg="-Xbootclasspath/p:${toString:lib.path.ref}"/&gt;
  685. &lt;/javac&gt;</pre>
  686. <h3>OpenJDK notes</h3>
  687. <p>The <a href="http://openjdk.java.net/" target="_top">OpenJDK</a> project has provided
  688. the <code>javac</code> <a href="http://openjdk.java.net/groups/compiler/" target="_top">compiler</a>
  689. as an open source project. The output of this project is a <code>javac.jar</code> which contains
  690. the <kbd>javac</kbd> compiler. This compiler may be used with the <code>&lt;javac&gt;</code> task
  691. with the use of a <kbd>-Xbootclasspath/p</kbd> Java argument. The argument needs to be given to the
  692. runtime system of the <kbd>javac</kbd> executable, so it needs to be prepended with a <kbd>-J</kbd>,
  693. for example:</p>
  694. <pre>
  695. &lt;property name="patched.javac.jar"
  696. location="${my.patched.compiler}/dist/lib/javac.jar"/&gt;
  697. &lt;presetdef name="patched.javac"&gt;
  698. &lt;javac fork="yes"&gt;
  699. &lt;compilerarg value="-J-Xbootclasspath/p:${patched.javac.jar}"/&gt;
  700. &lt;/javac&gt;
  701. &lt;/presetdef&gt;
  702. &lt;patched.javac srcdir="src/java" destdir="build/classes"
  703. debug="yes"/&gt;</pre>
  704. <h3>Note on package-info.java</h3>
  705. <p><samp>package-info.java</samp> files were introduced in Java 5 to allow package level
  706. annotations. On compilation, if the <code>.java</code> file does not contain runtime annotations,
  707. there will be no corresponding <code>.class</code> file. Prior to Ant 1.7.1, when
  708. the <code>&lt;javac&gt;</code> task is run again, the task will try to compile
  709. the <samp>package-info.java</samp> files again.</p>
  710. <p>With Ant 1.7.1, a different kind of logic was introduced that involved the timestamp of the
  711. directory that would normally contain the <samp>.class</samp> file. This logic turned out to lead
  712. to Ant not recompiling <samp>package-info.java</samp> in certain setup.</p>
  713. <p><em>Since Ant 1.8.0</em>, an "empty" <samp>package-info.class</samp> file is created if Ant
  714. compiles a <samp>package-info.java</samp> and no <samp>package-info.class</samp> file has been
  715. created by the compiler itself.</p>
  716. </body>
  717. </html>