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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Language" content="en-us">
  4. <title>Javac Task</title>
  5. </head>
  6. <body>
  7. <h2><a name="javac">Javac</a></h2>
  8. <h3>Description</h3>
  9. <p>Compiles a Java source tree.</p>
  10. <p>The source and destination directory will be recursively scanned for Java
  11. source files to compile. Only Java files that have no corresponding class file
  12. or where the class file is older than the java file will be compiled.</p>
  13. <p>Note: Ant uses only the names of the source and class files to find
  14. the classes that need a rebuild. It will not scan the source and therefor
  15. will have no knowledge about nested classes, classes that are named different
  16. from the source file and so on.</p>
  17. <p>The directory structure of the source tree should follow the package
  18. hierarchy.</p>
  19. <p>It is possible to refine the set of files that are being compiled/copied.
  20. This can be done with the <i>includes</i>, <i>includesfile</i>, <i>excludes</i>, <i>excludesfile</i> and <i>defaultexcludes</i>
  21. attributes. With the <i>includes</i> or <i>includesfile</i> attribute you specify the files you want to
  22. have included by using patterns. The <i>exclude</i> or <i>excludesfile</i> attribute is used to specify
  23. the files you want to have excluded. This is also done with patterns. And
  24. finally with the <i>defaultexcludes</i> attribute, you can specify whether you
  25. want to use default exclusions or not. See the section on <a
  26. href="../dirtasks.html#directorybasedtasks">directory based tasks</a>, on how the
  27. inclusion/exclusion of files works, and how to write patterns.</p>
  28. <p>It is possible to use different compilers. This can be selected with the
  29. &quot;build.compiler&quot; property or the compiler attribute. Here are the choices:-</p>
  30. <ul>
  31. <li>classic (the standard compiler of JDK 1.1/1.2) - javac1.1 and
  32. javac1.2 can be used as aliases</li>
  33. <li>modern (the standard compiler of JDK 1.3/1.4) - javac1.3 and
  34. javac1.4 can be used as aliases</li>
  35. <li>jikes (the <a
  36. href="http://oss.software.ibm.com/developerworks/opensource/jikes/project" target="_top">Jikes</a>
  37. compiler)</li>
  38. <li>jvc (the Command-Line Compiler from Microsoft's SDK for Java /
  39. Visual J++) - microsoft can be used as an alias</li>
  40. <li>kjc (the <a href="http://www.dms.at/kopi/" target="_top">kopi</a>
  41. compiler)</li>
  42. <li>gcj (the gcj compiler from gcc)</li>
  43. <li>sj (Symantec java compiler) - symantec can be used as an alias</li>
  44. <li>extJavac (run either modern or classic in a JVM of its own)</li>
  45. </ul>
  46. <p>For JDK 1.1/1.2, classic is the default. For JDK 1.3/1.4, modern is the default.
  47. If you wish to use a different compiler interface than those
  48. supplied, write a class that implements the CompilerAdapter interface
  49. (package org.apache.tools.ant.taskdefs.compilers). Supply the full
  50. classname in the &quot;build.compiler&quot; property.
  51. </p>
  52. <p>The fork attribute overrides the build.compiler setting and expects
  53. a JDK1.1 or higher to be set in java.home.
  54. </p>
  55. <p>This task will drop all entries that point to non-existent
  56. files/directories from the CLASSPATH it passes to the compiler.</p>
  57. <p><strong>Windows Note:</strong>When the modern compiler is used
  58. in unforked mode on Windows, it locks up the files present in the
  59. classpath of the &lt;javac&gt; task, and does not release them. The side
  60. effect of this is that you will not be able to delete or move
  61. those files later on in the build. The workaround is to fork when
  62. invoking the compiler.</p>
  63. </p>
  64. <h3>Parameters</h3>
  65. <table border="1" cellpadding="2" cellspacing="0">
  66. <tr>
  67. <td valign="top"><b>Attribute</b></td>
  68. <td valign="top"><b>Description</b></td>
  69. <td align="center" valign="top"><b>Required</b></td>
  70. </tr>
  71. <tr>
  72. <td valign="top">srcdir</td>
  73. <td valign="top">location of the java files. (See Notes at the end)</td>
  74. <td align="center" valign="top">Yes, unless nested <code>&lt;src&gt;</code> elements are present.</td>
  75. </tr>
  76. <tr>
  77. <td valign="top">destdir</td>
  78. <td valign="top">location to store the class files.</td>
  79. <td align="center" valign="top">No</td>
  80. </tr>
  81. <tr>
  82. <td valign="top">includes</td>
  83. <td valign="top">comma-separated list of patterns of files that must be
  84. included; all files are included when omitted.</td>
  85. <td valign="top" align="center">No</td>
  86. </tr>
  87. <tr>
  88. <td valign="top">includesfile</td>
  89. <td valign="top">the name of a file that contains
  90. include patterns.</td>
  91. <td valign="top" align="center">No</td>
  92. </tr>
  93. <tr>
  94. <td valign="top">excludes</td>
  95. <td valign="top">comma-separated list of patterns of files that must be
  96. excluded; no files (except default excludes) are excluded when omitted.</td>
  97. <td valign="top" align="center">No</td>
  98. </tr>
  99. <tr>
  100. <td valign="top">excludesfile</td>
  101. <td valign="top">the name of a file that contains
  102. exclude patterns.</td>
  103. <td valign="top" align="center">No</td>
  104. </tr>
  105. <tr>
  106. <td valign="top">defaultexcludes</td>
  107. <td valign="top">indicates whether default excludes should be used
  108. (<code>yes</code> | <code>no</code>); default excludes are used when omitted.</td>
  109. <td valign="top" align="center">No</td>
  110. </tr>
  111. <tr>
  112. <td valign="top">classpath</td>
  113. <td valign="top">the classpath to use.</td>
  114. <td align="center" valign="top">No</td>
  115. </tr>
  116. <tr>
  117. <td valign="top">sourcepath</td>
  118. <td valign="top">the sourcepath to use; defaults to the value of the srcdir attribute (or <code>&lt;src&gt;</code> elements).
  119. To suppress the sourcepath switch, use <code>sourcepath=&quot;&quot;</code>.</td>
  120. <td align="center" valign="top">No</td>
  121. </tr>
  122. <tr>
  123. <td valign="top">bootclasspath</td>
  124. <td valign="top">location of bootstrap class files.</td>
  125. <td align="center" valign="top">No</td>
  126. </tr>
  127. <tr>
  128. <td valign="top">classpathref</td>
  129. <td valign="top">the classpath to use, given as a
  130. <a href="../using.html#references">reference</a> to a PATH defined elsewhere.</td>
  131. <td align="center" valign="top">No</td>
  132. </tr>
  133. <tr>
  134. <td valign="top">sourcepathref</td>
  135. <td valign="top">the sourcepath to use, given as a
  136. <a href="../using.html#references">reference</a> to a PATH defined elsewhere.</td>
  137. <td align="center" valign="top">No</td>
  138. </tr>
  139. <tr>
  140. <td valign="top">bootclasspathref</td>
  141. <td valign="top">location of bootstrap class files, given as a
  142. <a href="../using.html#references">reference</a> to a PATH defined elsewhere.</td>
  143. <td align="center" valign="top">No</td>
  144. </tr>
  145. <tr>
  146. <td valign="top">extdirs</td>
  147. <td valign="top">location of installed extensions.</td>
  148. <td align="center" valign="top">No</td>
  149. </tr>
  150. <tr>
  151. <td valign="top">encoding</td>
  152. <td valign="top">encoding of source files. (gcj doesn't support
  153. this option yet)</td>
  154. <td align="center" valign="top">No</td>
  155. </tr>
  156. <tr>
  157. <td valign="top">nowarn</td>
  158. <td valign="top">indicates whether -nowarn switch should be passed
  159. to the compiler; defaults to <code>off</code>.</td>
  160. <td align="center" valign="top">No</td>
  161. </tr>
  162. <tr>
  163. <td valign="top">debug</td>
  164. <td valign="top">indicates whether source should be compiled with debug
  165. information; defaults to <code>off</code>.</td>
  166. <td align="center" valign="top">No</td>
  167. </tr>
  168. <tr>
  169. <td valign="top">optimize</td>
  170. <td valign="top">indicates whether source should be compiled with
  171. optimization; defaults to <code>off</code>.</td>
  172. <td align="center" valign="top">No</td>
  173. </tr>
  174. <tr>
  175. <td valign="top">deprecation</td>
  176. <td valign="top">indicates whether source should be compiled with
  177. deprecation information; defaults to <code>off</code>.</td>
  178. <td align="center" valign="top">No</td>
  179. </tr>
  180. <tr>
  181. <td valign="top">target</td>
  182. <td valign="top">generate class files for specific VM version (e.g.,
  183. <code>1.1</code> or <code>1.2</code>).</td>
  184. <td align="center" valign="top">No</td>
  185. </tr>
  186. <tr>
  187. <td valign="top">verbose</td>
  188. <td valign="top">asks the compiler for verbose output.</td>
  189. <td align="center" valign="top">No</td>
  190. </tr>
  191. <tr>
  192. <td valign="top">depend</td> <td valign="top">enables dependency-tracking
  193. for compilers that support this (jikes and classic)</td>
  194. <td align="center" valign="top">No</td>
  195. </tr>
  196. <tr>
  197. <td valign="top">includeAntRuntime</td>
  198. <td valign="top">whether to include the Ant run-time libraries;
  199. defaults to <code>yes</code>.</td>
  200. <td align="center" valign="top">No</td>
  201. </tr>
  202. <tr>
  203. <td valign="top">includeJavaRuntime</td>
  204. <td valign="top">whether to include the default run-time
  205. libraries from the executing VM; defaults to <code>no</code>.</td>
  206. <td align="center" valign="top">No</td>
  207. </tr>
  208. <tr>
  209. <td valign="top">fork</td>
  210. <td valign="top">whether to execute Javac using the JDK compiler
  211. externally; defaults to <code>no</code>. You can also give a
  212. complete path to the javac executable to use instead of
  213. <code>yes</code>, which would run the compiler of the Java
  214. vesrion that is currently running Ant.</td>
  215. <td align="center" valign="top">No</td>
  216. </tr>
  217. <tr>
  218. <td valign="top">memoryInitialSize</td>
  219. <td valign="top">the initial size of the memory for the underlying VM, if javac is run
  220. externally, ignored otherwise; defaults to the standard VM memory setting.
  221. (examples: <code>83886080</code>, <code>81920k</code>, or <code>80m</code>)</td>
  222. <td align="center" valign="top">No</td>
  223. </tr>
  224. <tr>
  225. <td valign="top">memoryMaximumSize</td>
  226. <td valign="top">the maximum size of the memory for the underlying VM, if javac is run
  227. externally, ignored otherwise; defaults to the standard VM memory setting.
  228. (examples: <code>83886080</code>, <code>81920k</code>, or <code>80m</code>)</td>
  229. <td align="center" valign="top">No</td>
  230. </tr>
  231. <tr>
  232. <td valign="top">failonerror</td> <td valign="top">
  233. indicates whether the build will continue even if there are compilation errors; defaults to <code>true</code>.
  234. </td>
  235. <td align="center" valign="top">No</td>
  236. </tr>
  237. <tr>
  238. <td valign="top">source</td>
  239. <td valign="top">Value of the <code>-source</code> command line
  240. switch, will be ignored by all implementations except
  241. <code>modern</code>, legal values are &quot;1.3&quot; and
  242. &quot;1.4&quot; - by default, no <code>-source</code> argument
  243. will be used at all.</td>
  244. <td align="center" valign="top">No</td>
  245. </tr>
  246. <tr>
  247. <td valign="top">debuglevel</td>
  248. <td valign="top">Keyword list to be appended to the <code>-g</code> command line
  249. switch. This will be ignored by all implementations except
  250. <code>modern</code> and <code>classic(ver >= 1.2)</code>, legal values are
  251. &quot;none&quot; or a comma separated list of the following keywords.
  252. Valid keywords are &quot;lines&quot;, &quot;vars&quot; and &quot;source&quot;
  253. - if debuglevel is not specified, by default, nothing will be
  254. appended to <code>-g</code>. If debug is not turned on, this attribute will be
  255. ignored.
  256. </td>
  257. <td align="center" valign="top">No</td>
  258. </tr>
  259. <tr>
  260. <td valign="top">compiler</td>
  261. <td valign="top">The compiler implementation to use.
  262. Defaults to the value of the <code>build.compiler</code> property.</td>
  263. <td align="center" valign="top">No</td>
  264. </tr>
  265. </table>
  266. <h3>Parameters specified as nested elements</h3>
  267. <p>This task forms an implicit <a href="../CoreTypes/fileset.html">FileSet</a> and
  268. supports all attributes of <code>&lt;fileset&gt;</code>
  269. (<code>dir</code> becomes <code>srcdir</code>) as well as the nested
  270. <code>&lt;include&gt;</code>, <code>&lt;exclude&gt;</code> and
  271. <code>&lt;patternset&gt;</code> elements.</p>
  272. <h4><code>src</code>, <code>classpath</code>, <code>sourcepath</code>,
  273. <code>bootclasspath</code> and <code>extdirs</code></h4>
  274. <p><code>Javac</code>'s <i>srcdir</i>, <i>classpath</i>,
  275. <i>sourcepath</i>, <i>bootclasspath</i> and <i>extdirs</i> attributes are <a
  276. href="../using.html#path">path-like structures</a> and can also be set via nested
  277. <code>&lt;src&gt;</code>,
  278. <code>&lt;classpath&gt;</code>,
  279. <code>&lt;sourcepath&gt;</code>,
  280. <code>&lt;bootclasspath&gt;</code> and
  281. <code>&lt;extdirs&gt;</code> elements, respectively.</p>
  282. <h4>compilerarg</h4>
  283. <p>You can specify additional command line arguments for the compiler
  284. with nested <code>&lt;compilerarg&gt;</code> elements. These elements
  285. are specified like <a href="../using.html#arg">Command-line
  286. Arguments</a> but have an additional attribute that can be used to
  287. enable arguments only if a given compiler implementation will be
  288. used.</p>
  289. <table border="1" cellpadding="2" cellspacing="0">
  290. <tr>
  291. <td width="12%" valign="top"><b>Attribute</b></td>
  292. <td width="78%" valign="top"><b>Description</b></td>
  293. <td width="10%" valign="top"><b>Required</b></td>
  294. </tr>
  295. <tr>
  296. <td valign="top">value</td>
  297. <td align="center" rowspan="4">See <a href="../using.html#arg">here</a></td>
  298. <td align="center" rowspan="4">Exactly one of these.</td>
  299. </tr>
  300. <tr>
  301. <td valign="top">line</td>
  302. </tr>
  303. <tr>
  304. <td valign="top">file</td>
  305. </tr>
  306. <tr>
  307. <td valign="top">path</td>
  308. </tr>
  309. <tr>
  310. <td valign="top">implementation</td>
  311. <td align="center">Only use this argument if the chosen
  312. implementation matches this attribute. Possible choices are the
  313. same as those of the build.compiler property.</td>
  314. <td align="center">No.</td>
  315. </tr>
  316. </table>
  317. <h3>Examples</h3>
  318. <pre> &lt;javac srcdir=&quot;${src}&quot;
  319. destdir=&quot;${build}&quot;
  320. classpath=&quot;xyz.jar&quot;
  321. debug=&quot;on&quot;
  322. /&gt;</pre>
  323. <p>compiles all <code>.java</code> files under the <code>${src}</code>
  324. directory, and stores
  325. the <code>.class</code> files in the <code>${build}</code> directory.
  326. The classpath used contains <code>xyz.jar</code>, and debug information is on.</p>
  327. <pre> &lt;javac srcdir=&quot;${src}&quot;
  328. destdir=&quot;${build}&quot;
  329. fork=&quot;true&quot;
  330. /&gt;</pre>
  331. <p>compiles all <code>.java</code> files under the <code>${src}</code>
  332. directory, and stores the <code>.class</code> files in the
  333. <code>${build}</code> directory. This will fork off the javac
  334. compiler using the default javac executable.</p>
  335. <pre> &lt;javac srcdir=&quot;${src}&quot;
  336. destdir=&quot;${build}&quot;
  337. fork=&quot;java$$javac.exe&quot;
  338. /&gt;</pre>
  339. <p>compiles all <code>.java</code> files under the <code>${src}</code>
  340. directory, and stores the <code>.class</code> files in the
  341. <code>${build}</code> directory. This will fork off the javac
  342. compiler using the executable named <code>java$javac.exe</code>. Note
  343. that the <code>$</code> sign needs to be escaped by a second one.</p>
  344. <pre> &lt;javac srcdir=&quot;${src}&quot;
  345. destdir=&quot;${build}&quot;
  346. includes=&quot;mypackage/p1/**,mypackage/p2/**&quot;
  347. excludes=&quot;mypackage/p1/testpackage/**&quot;
  348. classpath=&quot;xyz.jar&quot;
  349. debug=&quot;on&quot;
  350. /&gt;</pre>
  351. <p>compiles <code>.java</code> files under the <code>${src}</code>
  352. directory, and stores the
  353. <code>.class</code> files in the <code>${build}</code> directory.
  354. The classpath used contains <code>xyz.jar</code>, and debug information is on.
  355. Only files under <code>mypackage/p1</code> and <code>mypackage/p2</code> are
  356. used. Files in the <code>mypackage/p1/testpackage</code> directory are excluded
  357. from compilation.</p>
  358. <pre> &lt;javac srcdir=&quot;${src}:${src2}&quot;
  359. destdir=&quot;${build}&quot;
  360. includes=&quot;mypackage/p1/**,mypackage/p2/**&quot;
  361. excludes=&quot;mypackage/p1/testpackage/**&quot;
  362. classpath=&quot;xyz.jar&quot;
  363. debug=&quot;on&quot;
  364. /&gt;</pre>
  365. <p>is the same as the previous example, with the addition of a second
  366. source path, defined by
  367. the property <code>src2</code>. This can also be represented using nested
  368. <code>&lt;src&gt;</code> elements as follows:</p>
  369. <pre> &lt;javac destdir=&quot;${build}&quot;
  370. classpath=&quot;xyz.jar&quot;
  371. debug=&quot;on&quot;&gt;
  372. &lt;src path=&quot;${src}&quot;/&gt;
  373. &lt;src path=&quot;${src2}&quot;/&gt;
  374. &lt;include name=&quot;mypackage/p1/**&quot;/&gt;
  375. &lt;include name=&quot;mypackage/p2/**&quot;/&gt;
  376. &lt;exclude name=&quot;mypackage/p1/testpackage/**&quot;/&gt;
  377. &lt;/javac&gt;</pre>
  378. <p><b>Note:</b> If you are using Ant on Windows and a new DOS window pops up
  379. for every use of an external compiler, this may be a problem of the JDK you are using.
  380. This problem may occur with all JDKs &lt; 1.2.</p>
  381. <p><b>Note:</b> If you wish to compile only source-files located in some packages below a
  382. common root you should not include these packages in the srcdir-attribute. Use include/exclude-attributes
  383. or elements to filter for these packages. If you include part of your package-structure inside the srcdir-attribute
  384. (or nested src-elements) Ant will start to recompile your sources every time you call it.</p>
  385. <h3>Jikes Notes</h3>
  386. Jikes supports some extra options, which can be set be defining
  387. properties prior to invoking the task. The ant developers are aware that
  388. this is ugly and inflexible -expect a better solution in the future. All
  389. the options are boolean, and must be set to &quot;true&quot; or &quot;yes&quot; to be
  390. interpreted as anything other than false; by default
  391. build.compiler.warnings is &quot;true&quot; while all others are &quot;false&quot;</p>
  392. <table border="1" cellpadding="2" cellspacing="0">
  393. <tr>
  394. <td valign="top">
  395. build.compiler.emacs
  396. </td>
  397. <td valign="top">
  398. Enable emacs compatible error messages
  399. </td>
  400. </tr>
  401. <tr>
  402. <td valign="top">
  403. build.compiler.warnings<br>
  404. <b>This property has been deprecated, use the nowarn attribute
  405. instead</b>
  406. </td>
  407. <td valign="top">
  408. don't disable warning messages
  409. </td>
  410. </tr>
  411. <tr>
  412. <td valign="top">
  413. build.compiler.pedantic
  414. </td>
  415. <td valign="top">
  416. enable pedantic warnings
  417. </td>
  418. </tr>
  419. <tr>
  420. <td valign="top">
  421. build.compiler.fulldepend
  422. </td>
  423. <td valign="top">
  424. enable full dependency checking,<br>
  425. &quot;+F&quot; in the jikes manual.
  426. </td>
  427. </tr>
  428. </table>
  429. <hr>
  430. <p align="center">Copyright &copy; 2001-2002 Apache Software Foundation. All rights
  431. Reserved.</p>
  432. </body>
  433. </html>