You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

javac.html 35 KiB

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