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

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