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.

java.html 14 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Language" content="en-us">
  4. <title>Java Task</title>
  5. </head>
  6. <body>
  7. <h2><a name="java">Java</a></h2>
  8. <h3>Description</h3>
  9. <p>Executes a Java class within the running (Ant) VM or forks another VM if
  10. specified.</p>
  11. <p>
  12. If odd things go wrong when you run this task, set fork="true" to use a new
  13. JVM.
  14. <p>As of Ant 1.7, you can interact with a forked VM, as well as
  15. sending input to it via the <code>input</code> and <code>inputstring</code>
  16. attributes.</p>
  17. <h3>Parameters</h3>
  18. <table border="1" cellpadding="2" cellspacing="0">
  19. <tr>
  20. <td valign="top"><b>Attribute</b></td>
  21. <td valign="top"><b>Description</b></td>
  22. <td align="center" valign="top"><b>Required</b></td>
  23. </tr>
  24. <tr>
  25. <td valign="top">classname</td>
  26. <td valign="top">the Java class to execute.</td>
  27. <td align="center" valign="top">Either <tt>jar</tt> or <tt>classname</tt></td>
  28. </tr>
  29. <tr>
  30. <td valign="top">jar</td>
  31. <td valign="top">the location of the jar file to execute (must have a
  32. Main-Class entry in the manifest). Fork must be set to true if this option is selected.
  33. See notes below for more details.
  34. </td>
  35. <td align="center" valign="top">Either <tt>jar</tt> or <tt>classname</tt></td>
  36. </tr>
  37. <tr>
  38. <td valign="top">args</td>
  39. <td valign="top">the arguments for the class that is
  40. executed. <b>deprecated, use nested <code>&lt;arg&gt;</code>
  41. elements instead.</b></td>
  42. <td align="center" valign="top">No</td>
  43. </tr>
  44. <tr>
  45. <td valign="top">classpath</td>
  46. <td valign="top">the classpath to use.</td>
  47. <td align="center" valign="top">No</td>
  48. </tr>
  49. <tr>
  50. <td valign="top">classpathref</td>
  51. <td valign="top">the classpath to use, given as <a
  52. href="../using.html#references">reference</a> to a PATH defined elsewhere.</td>
  53. <td align="center" valign="top">No</td>
  54. </tr>
  55. <tr>
  56. <td valign="top">fork</td>
  57. <td valign="top">if enabled triggers the class execution in another VM
  58. (disabled by default)</td>
  59. <td align="center" valign="top">No</td>
  60. </tr>
  61. <tr>
  62. <td valign="top">spawn</td>
  63. <td valign="top">if enabled allows to start a process which will outlive ant.<br/>
  64. Requires fork=true, and not compatible
  65. with timeout, input, output, error, result attributes.<br/>
  66. (disabled by default)</td>
  67. <td align="center" valign="top">No</td>
  68. </tr>
  69. <tr>
  70. <td valign="top">jvm</td>
  71. <td valign="top">the command used to invoke the Java Virtual Machine,
  72. default is 'java'. The command is resolved by java.lang.Runtime.exec().
  73. Ignored if fork is disabled.
  74. </td>
  75. <td align="center" valign="top">No</td>
  76. </tr>
  77. <tr>
  78. <td valign="top">jvmargs</td>
  79. <td valign="top">the arguments to pass to the forked VM (ignored
  80. if fork is disabled). <b>deprecated, use nested
  81. <code>&lt;jvmarg&gt;</code> elements instead.</b></td>
  82. <td align="center" valign="top">No</td>
  83. </tr>
  84. <tr>
  85. <td valign="top">maxmemory</td>
  86. <td valign="top">Max amount of memory to allocate to the forked VM
  87. (ignored if fork is disabled)</td>
  88. <td align="center" valign="top">No</td>
  89. </tr>
  90. <tr>
  91. <td valign="top">failonerror</td>
  92. <td valign="top">Stop the buildprocess if the command exits with a
  93. returncode other than 0. Default is "false" (see <a href="#failonerror">note</a>)</td>
  94. <td align="center" valign="top">No</td>
  95. </tr>
  96. <tr>
  97. <td valign="top">resultproperty</td>
  98. <td valign="top">The name of a property in which the return code of the
  99. command should be stored. Only of interest if failonerror=false
  100. and if fork=true.</td>
  101. <td align="center" valign="top">No</td>
  102. </tr>
  103. <tr>
  104. <td valign="top">dir</td>
  105. <td valign="top">The directory to invoke the VM in. (ignored if
  106. fork is disabled)</td>
  107. <td align="center" valign="top">No</td>
  108. </tr>
  109. <tr>
  110. <td valign="top">output</td>
  111. <td valign="top">Name of a file to which to write the output. If the error stream
  112. is not also redirected to a file or property, it will appear in this output.</td>
  113. <td align="center" valign="top">No</td>
  114. </tr>
  115. <tr>
  116. <td valign="top">error</td>
  117. <td valign="top">The file to which the standard error of the command should be
  118. redirected. </td>
  119. <td align="center" valign="top">No</td>
  120. </tr>
  121. <tr>
  122. <td valign="top">logError</td>
  123. <td valign="top">This attribute is used when you wish to see error output in Ant's
  124. log and you are redirecting output to a file/property. The error
  125. output will not be included in the output file/property. If you
  126. redirect error with the &quot;error&quot; or &quot;errorProperty&quot;
  127. attributes, this will have no effect.</td>
  128. <td align="center" valign="top">No</td>
  129. </tr>
  130. <tr>
  131. <td valign="top">append</td>
  132. <td valign="top">Whether output and error files should be appended to or overwritten.
  133. Defaults to false.</td>
  134. <td align="center" valign="top">No</td>
  135. </tr>
  136. <tr>
  137. <td valign="top">outputproperty</td>
  138. <td valign="top">The name of a property in which the output of the
  139. command should be stored. Unless the error stream is redirected to a separate
  140. file or stream, this property will include the error output.</td>
  141. <td align="center" valign="top">No</td>
  142. </tr>
  143. <tr>
  144. <td valign="top">errorproperty</td>
  145. <td valign="top">The name of a property in which the standard error of the
  146. command should be stored.</td>
  147. <td align="center" valign="top">No</td>
  148. </tr>
  149. <tr>
  150. <td valign="top">input</td>
  151. <td valign="top">A file from which the executed command's standard input
  152. is taken. This attribute is mutually exclusive with the
  153. inputstring attribute</td>
  154. <td align="center" valign="top">No; default is to take standard input from console
  155. (unless <code>spawn="true"</code>)</td>
  156. </tr>
  157. <tr>
  158. <td valign="top">inputstring</td>
  159. <td valign="top">A string which serves as the input stream for the
  160. executed command. This attribute is mutually exclusive with the
  161. input attribute.</td>
  162. <td align="center" valign="top">No; default is to take standard input from console
  163. (unless <code>spawn="true"</code>)</td>
  164. </tr>
  165. <tr>
  166. <td valign="top">newenvironment</td>
  167. <td valign="top">Do not propagate old environment when new
  168. environment variables are specified. Default is &quot;false&quot;
  169. (ignored if fork is disabled).</td>
  170. <td align="center" valign="top">No</td>
  171. </tr>
  172. <tr>
  173. <td valign="top">timeout</td>
  174. <td valign="top">Stop the command if it doesn't finish within the
  175. specified time (given in milliseconds). <strong>It is highly
  176. recommended to use this feature only if fork is enabled.</strong></td>
  177. <td align="center" valign="top">No</td>
  178. </tr>
  179. <tr>
  180. <td valign="top">clonevm</td>
  181. <td valign="top">If set to true true, then all system properties
  182. and the bootclasspath of the forked Java Virtual Machine will be
  183. the same as those of the Java VM running Ant. Default is
  184. &quot;false&quot; (ignored if fork is disabled).
  185. <em>since Ant 1.7</em></td>
  186. <td align="center" valign="top">No</td>
  187. </tr>
  188. </table>
  189. <h3>Parameters specified as nested elements</h3>
  190. <h4>arg and jvmarg</h4>
  191. <p>Use nested <code>&lt;arg&gt;</code> and <code>&lt;jvmarg&gt;</code>
  192. elements to specify arguments for the Java class and the forked VM respectively.
  193. See <a href="../using.html#arg">Command line arguments</a>.</p>
  194. <h4>sysproperty</h4>
  195. <p>Use nested <code>&lt;sysproperty&gt;</code>
  196. elements to specify system properties required by the class.
  197. These properties will be made available to the VM during the execution
  198. of the class (either ANT's VM or the forked VM). The attributes
  199. for this element are the same as for <a href="exec.html#env">environment
  200. variables</a>.</p>
  201. <h4>syspropertyset</h4>
  202. <p>You can specify a set of properties to be used as system properties
  203. with <a href="../CoreTypes/propertyset.html">syspropertyset</a>s.</p>
  204. <p><em>since Ant 1.6</em>.</p>
  205. <h4>classpath</h4>
  206. <p><code>Java</code>'s <i>classpath</i> attribute is a <a
  207. href="../using.html#path">PATH like structure</a> and can also be set via a nested
  208. <i>classpath</i> element.</p>
  209. <h4>bootclasspath</h4>
  210. <p>The location of bootstrap class files can be specified using this
  211. <a href="../using.html#path">PATH like structure</a> - will be ignored
  212. if <i>fork</i> is not <code>true</code> or the target VM doesn't
  213. support it (i.e. Java 1.1).</p>
  214. <p><em>since Ant 1.6</em>.</p>
  215. <h4>env</h4>
  216. <p>It is possible to specify environment variables to pass to the
  217. forked VM via nested <i>env</i> elements. See the description in the
  218. section about <a href="exec.html#env">exec</a></p>
  219. <p>Settings will be ignored if fork is disabled.</p>
  220. <h4>permissions</h4>
  221. <p>Security permissions can be revoked and granted during the execution of the
  222. class via a nested <i>permissions</i> element. For more information please
  223. see <a href="../CoreTypes/permissions.html">permissions</a></p>
  224. <p>When the permission RuntimePermission exitVM has not been granted (or has
  225. been revoked) the System.exit() call will be intercepted
  226. and treated like indicated in <i>failonerror</i>.</p>
  227. <p><a name="failonerror">Note:</a>
  228. if you specify <code>failonerror=&quot;true&quot;</code> and you do not specify permissions,
  229. a set of default permissions will be added to your Java invocation to make sure that
  230. a non-zero return code will lead to a <code>BuildException</code>.
  231. </p>
  232. <p>Settings will be ignored if fork is enabled.</p>
  233. <p><em>since Ant 1.6</em>.</p>
  234. <h4>assertions</h4>
  235. <p>You can control enablement of Java 1.4 assertions with an
  236. <a href="../CoreTypes/assertions.html"><tt>&lt;assertions&gt;</tt></a>
  237. subelement.</p>
  238. <p>Assertion statements are currently ignored in non-forked mode.</p>
  239. <p><em>since Ant 1.6.</em></p>
  240. <a name="redirector"><h4>redirector</h4></a>
  241. <i><b>Since Ant 1.6.2</b></i>
  242. <p>A nested <a href="../CoreTypes/redirector.html">I/O Redirector</a>
  243. can be specified. In general, the attributes of the redirector behave
  244. as the corresponding attributes available at the task level. The most
  245. notable peculiarity stems from the retention of the &lt;java&gt;
  246. attributes for backwards compatibility. Any file mapping is done
  247. using a <CODE>null</CODE> sourcefile; therefore not all
  248. <a href="../CoreTypes/mapper.html">Mapper</a> types will return
  249. results. When no results are returned, redirection specifications
  250. will fall back to the task level attributes. In practice this means that
  251. defaults can be specified for input, output, and error output files.
  252. </p>
  253. <h3>Errors and return codes</h3>
  254. By default the return code of a <code>&lt;java&gt;</code> is ignored. Alternatively, you can set <code>resultproperty</code> to the name
  255. of a property and have it assigned to the result code (barring immutability,
  256. of course).
  257. When you set <code>failonerror="true"</code>, the only possible value for <code>resultproperty</code> is 0. Any non zero response is treated as an
  258. error and would mean the build exits.
  259. <p> Similarly, if <code>failonerror="false"</code> and <code>fork="false"</code>
  260. , then <code>&lt;java&gt;</code> <b>must</b> return 0 otherwise the build will exit, as the class was run by the build jvm.</p>
  261. <h3>JAR file execution</h3>
  262. The parameter of the <tt>jar</tt> attribute is of type <tt>File</tt>;
  263. that is, the parameter is resolved to an absolute file relative to the
  264. base directory of the project, <i>not</i> the directory in which the Java
  265. task is run. If you need to locate a JAR file relative to the directory
  266. the task will be run in, you need to explicitly create the full path
  267. to the JAR file.
  268. <h3>Examples</h3>
  269. <pre>
  270. &lt;java classname=&quot;test.Main&quot;&gt;
  271. &lt;arg value=&quot;-h&quot;/&gt;
  272. &lt;classpath&gt;
  273. &lt;pathelement location=&quot;dist/test.jar&quot;/&gt;
  274. &lt;pathelement path=&quot;${java.class.path}&quot;/&gt;
  275. &lt;/classpath&gt;
  276. &lt;/java&gt;
  277. </pre>
  278. Run a class in this JVM with a new jar on the classpath
  279. <pre> &lt;java jar=&quot;dist/test.jar&quot;
  280. fork="true"
  281. failonerror="true"
  282. maxmemory="128m"
  283. &gt;
  284. &lt;arg value=&quot;-h&quot;/&gt;
  285. &lt;classpath&gt;
  286. &lt;pathelement location=&quot;dist/test.jar&quot;/&gt;
  287. &lt;pathelement path=&quot;${java.class.path}&quot;/&gt;
  288. &lt;/classpath&gt;
  289. &lt;/java&gt;
  290. </pre>
  291. Run the JAR test.jar in this project's dist/lib directory.
  292. using the manifest supplied entry point, forking (as required),
  293. and with a maximum memory of 128MB. Any non zero return code breaks the build.
  294. <pre> &lt;java
  295. dir="${exec.dir}"
  296. jar=&quot;${exec.dir}/dist/test.jar&quot;
  297. fork="true"
  298. failonerror="true"
  299. maxmemory="128m"
  300. &gt;
  301. &lt;arg value=&quot;-h&quot;/&gt;
  302. &lt;classpath&gt;
  303. &lt;pathelement location=&quot;dist/test.jar&quot;/&gt;
  304. &lt;pathelement path=&quot;${java.class.path}&quot;/&gt;
  305. &lt;/classpath&gt;
  306. &lt;/java&gt;
  307. </pre>
  308. Run the JAR dist/test.jar relative to the directory
  309. <tt>${exec.dir}</tt>, this being the same directory into which the JVM
  310. is to start up.
  311. <pre> &lt;java classname=&quot;test.Main&quot;/&gt;</pre>
  312. <pre> &lt;java classname=&quot;test.Main&quot;
  313. fork=&quot;yes&quot; &gt;
  314. &lt;sysproperty key=&quot;DEBUG&quot; value=&quot;true&quot;/&gt;
  315. &lt;arg value=&quot;-h&quot;/&gt;
  316. &lt;jvmarg value=&quot;-Xrunhprof:cpu=samples,file=log.txt,depth=3&quot;/&gt;
  317. &lt;/java&gt;
  318. </pre>
  319. <strong>Note</strong>: you can not specify the (highly deprecated) MSJVM, "jview.exe" as the
  320. JVM, as it takes different parameters for other JVMs,
  321. That JVM can be started from <code>&lt;exec&gt;</code> if required.
  322. <hr>
  323. <p align="center">Copyright &copy; 2000-2005 The Apache Software Foundation. All rights
  324. Reserved.</p>
  325. </body>
  326. </html>