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

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