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

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