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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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>Note that you cannot interact with a forked VM, the only way to
  15. send input to it is via the input and inputstring attributes.</p>
  16. <h3>Parameters</h3>
  17. <table border="1" cellpadding="2" cellspacing="0">
  18. <tr>
  19. <td valign="top"><b>Attribute</b></td>
  20. <td valign="top"><b>Description</b></td>
  21. <td align="center" valign="top"><b>Required</b></td>
  22. </tr>
  23. <tr>
  24. <td valign="top">classname</td>
  25. <td valign="top">the Java class to execute.</td>
  26. <td align="center" valign="top">Either jar or classname</td>
  27. </tr>
  28. <tr>
  29. <td valign="top">jar</td>
  30. <td valign="top">the location of the jar file to execute (must have a
  31. Main-Class entry in the manifest). Fork must be set to true if this option is selected.</td>
  32. <td align="center" valign="top">Either jar or classname</td>
  33. </tr>
  34. <tr>
  35. <td valign="top">args</td>
  36. <td valign="top">the arguments for the class that is
  37. executed. <b>deprecated, use nested <code>&lt;arg&gt;</code>
  38. elements instead.</b></td>
  39. <td align="center" valign="top">No</td>
  40. </tr>
  41. <tr>
  42. <td valign="top">classpath</td>
  43. <td valign="top">the classpath to use.</td>
  44. <td align="center" valign="top">No</td>
  45. </tr>
  46. <tr>
  47. <td valign="top">classpathref</td>
  48. <td valign="top">the classpath to use, given as <a
  49. href="../using.html#references">reference</a> to a PATH defined elsewhere.</td>
  50. <td align="center" valign="top">No</td>
  51. </tr>
  52. <tr>
  53. <td valign="top">fork</td>
  54. <td valign="top">if enabled triggers the class execution in another VM
  55. (disabled by default)</td>
  56. <td align="center" valign="top">No</td>
  57. </tr>
  58. <tr>
  59. <td valign="top">spawn</td>
  60. <td valign="top">if enabled allows to start a process which will outlive ant.<br/>
  61. Requires fork=true, and not compatible
  62. with timeout, input, output, error, result attributes.<br/>
  63. (disabled by default)</td>
  64. <td align="center" valign="top">No</td>
  65. </tr>
  66. <tr>
  67. <td valign="top">jvm</td>
  68. <td valign="top">the command used to invoke the Java Virtual Machine,
  69. default is 'java'. The command is resolved by java.lang.Runtime.exec().
  70. Ignored if fork is disabled.
  71. </td>
  72. <td align="center" valign="top">No</td>
  73. </tr>
  74. <tr>
  75. <td valign="top">jvmargs</td>
  76. <td valign="top">the arguments to pass to the forked VM (ignored
  77. if fork is disabled). <b>deprecated, use nested
  78. <code>&lt;jvmarg&gt;</code> elements instead.</b></td>
  79. <td align="center" valign="top">No</td>
  80. </tr>
  81. <tr>
  82. <td valign="top">maxmemory</td>
  83. <td valign="top">Max amount of memory to allocate to the forked VM
  84. (ignored if fork is disabled)</td>
  85. <td align="center" valign="top">No</td>
  86. </tr>
  87. <tr>
  88. <td valign="top">failonerror</td>
  89. <td valign="top">Stop the buildprocess if the command exits with a
  90. returncode other than 0. Default is "false" (see <a href="#failonerror">note)</a></td>
  91. <td align="center" valign="top">No</td>
  92. </tr>
  93. <tr>
  94. <td valign="top">resultproperty</td>
  95. <td valign="top">The name of a property in which the return code of the
  96. command should be stored. Only of interest if failonerror=false
  97. and if fork=true.</td>
  98. <td align="center" valign="top">No</td>
  99. </tr>
  100. <tr>
  101. <td valign="top">dir</td>
  102. <td valign="top">The directory to invoke the VM in. (ignored if
  103. fork is disabled)</td>
  104. <td align="center" valign="top">No</td>
  105. </tr>
  106. <tr>
  107. <td valign="top">output</td>
  108. <td valign="top">Name of a file to which to write the output. If the error stream
  109. is not also redirected to a file or property, it will appear in this output.</td>
  110. <td align="center" valign="top">No</td>
  111. </tr>
  112. <tr>
  113. <td valign="top">error</td>
  114. <td valign="top">The file to which the standard error of the command should be
  115. redirected. </td>
  116. <td align="center" valign="top">No</td>
  117. </tr>
  118. <tr>
  119. <td valign="top">logError</td>
  120. <td valign="top">This attribute is used when you wish to see error output in Ant's
  121. log and you are redirecting output to a file/property. The error
  122. output will not be included in the output file/property. If you
  123. redirect error with the &quot;error&quot; or &quot;errorProperty&quot;
  124. attributes, this will have no effect.</td>
  125. <td align="center" valign="top">No</td>
  126. </tr>
  127. <tr>
  128. <td valign="top">append</td>
  129. <td valign="top">Whether output and error files should be appended to or overwritten.
  130. Defaults to false.</td>
  131. <td align="center" valign="top">No</td>
  132. </tr>
  133. <tr>
  134. <td valign="top">outputproperty</td>
  135. <td valign="top">The name of a property in which the output of the
  136. command should be stored. Unless the error stream is redirected to a separate
  137. file or stream, this property will include the error output.</td>
  138. <td align="center" valign="top">No</td>
  139. </tr>
  140. <tr>
  141. <td valign="top">errorproperty</td>
  142. <td valign="top">The name of a property in which the standard error of the
  143. command should be stored.</td>
  144. <td align="center" valign="top">No</td>
  145. </tr>
  146. <tr>
  147. <td valign="top">input</td>
  148. <td valign="top">A file from which the executed command's standard input
  149. is taken. This attribute is mutually exclusive with the
  150. inputstring attribute</td>
  151. <td align="center" valign="top">No</td>
  152. </tr>
  153. <tr>
  154. <td valign="top">inputstring</td>
  155. <td valign="top">A string which serves as the input stream for the
  156. executed command. This attribute is mutually exclusive with the
  157. input attribute.</td>
  158. <td align="center" valign="top">No</td>
  159. </tr>
  160. <tr>
  161. <td valign="top">newenvironment</td>
  162. <td valign="top">Do not propagate old environment when new
  163. environment variables are specified. Default is &quot;false&quot;
  164. (ignored if fork is disabled).</td>
  165. <td align="center" valign="top">No</td>
  166. </tr>
  167. <tr>
  168. <td valign="top">timeout</td>
  169. <td valign="top">Stop the command if it doesn't finish within the
  170. specified time (given in milliseconds). <strong>It is highly
  171. recommended to use this feature only if fork is enabled.</strong></td>
  172. <td align="center" valign="top">No</td>
  173. </tr>
  174. </table>
  175. <h3>Parameters specified as nested elements</h3>
  176. <h4>arg and jvmarg</h4>
  177. <p>Use nested <code>&lt;arg&gt;</code> and <code>&lt;jvmarg&gt;</code>
  178. elements to specify arguments for the Java class and the forked VM respectively.
  179. See <a href="../using.html#arg">Command line arguments</a>.</p>
  180. <h4>sysproperty</h4>
  181. <p>Use nested <code>&lt;sysproperty&gt;</code>
  182. elements to specify system properties required by the class.
  183. These properties will be made available to the VM during the execution
  184. of the class (either ANT's VM or the forked VM). The attributes
  185. for this element are the same as for <a href="exec.html#env">environment
  186. variables</a>.</p>
  187. <h4>syspropertyset</h4>
  188. <p>You can specify a set of properties to be used as system properties
  189. with <a href="../CoreTypes/propertyset.html">syspropertyset</a>s.</p>
  190. <p><em>since Ant 1.6</em>.</p>
  191. <h4>classpath</h4>
  192. <p><code>Java</code>'s <i>classpath</i> attribute is a <a
  193. href="../using.html#path">PATH like structure</a> and can also be set via a nested
  194. <i>classpath</i> element.</p>
  195. <h4>bootclasspath</h4>
  196. <p>The location of bootstrap class files can be specified using this
  197. <a href="../using.html#path">PATH like structure</a> - will be ignored
  198. if <i>fork</i> is not <code>true</code> or the target VM doesn't
  199. support it (i.e. Java 1.1).</p>
  200. <p><em>since Ant 1.6</em>.</p>
  201. <h4>env</h4>
  202. <p>It is possible to specify environment variables to pass to the
  203. forked VM via nested <i>env</i> elements. See the description in the
  204. section about <a href="exec.html#env">exec</a></p>
  205. <p>Settings will be ignored if fork is disabled.</p>
  206. <h4>permissions</h4>
  207. <p>Security permissions can be revoked and granted during the execution of the
  208. class via a nested <i>permissions</i> element. For more information please
  209. see <a href="../CoreTypes/permissions.html">permissions</a></p>
  210. <p>When the permission RuntimePermission exitVM has not been granted (or has
  211. been revoked) the System.exit() call will be intercepted
  212. and treated like indicated in <i>failonerror</i>.</p>
  213. <p><a name="failonerror"/>
  214. If you specify <code>failonerror=&quot;true&quot;</code> and you do not specify permissions,
  215. a set of default permissions will be added to your java invocation making sure that
  216. a non zero return code will lead to a <code>BuildException</code>.
  217. </p>
  218. <p>Settings will be ignored if fork is enabled.</p>
  219. <p><em>since Ant 1.6</em>.</p>
  220. <h3>Errors and return codes</h3>
  221. By default the return code of a &lt;java&gt; is ignored. Alternatively, you can set <code>resultproperty</code> to the name
  222. of a property and have it assigned to the result code (barring immutability,
  223. of course).
  224. 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
  225. error and would mean the build exits.
  226. <p> Similarly, if <code>failonerror="false"</code> and <code>fork="false"</code>
  227. , 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>
  228. <h3>Examples</h3>
  229. <pre>
  230. &lt;java classname=&quot;test.Main&quot;&gt;
  231. &lt;arg value=&quot;-h&quot;/&gt;
  232. &lt;classpath&gt;
  233. &lt;pathelement location=&quot;dist/test.jar&quot;/&gt;
  234. &lt;pathelement path=&quot;${java.class.path}&quot;/&gt;
  235. &lt;/classpath&gt;
  236. &lt;/java&gt;
  237. </pre>
  238. Run a class in this JVM with a new jar on the classpath
  239. <pre> &lt;java jar=&quot;dist/test.jar&quot;
  240. fork="true"
  241. failonerror="true"
  242. maxmemory="128m"
  243. &gt;
  244. &lt;arg value=&quot;-h&quot;/&gt;
  245. &lt;classpath&gt;
  246. &lt;pathelement location=&quot;dist/test.jar&quot;/&gt;
  247. &lt;pathelement path=&quot;${java.class.path}&quot;/&gt;
  248. &lt;/classpath&gt;
  249. &lt;/java&gt;
  250. </pre>
  251. Run the jar using the manifest supplied entry point, forking (as required),
  252. and with a maximum memory of 128MB. Any non zero return code breaks the build.
  253. <pre> &lt;java classname=&quot;test.Main&quot;/&gt;</pre>
  254. <pre> &lt;java classname=&quot;test.Main&quot;
  255. fork=&quot;yes&quot; &gt;
  256. &lt;sysproperty key=&quot;DEBUG&quot; value=&quot;true&quot;/&gt;
  257. &lt;arg value=&quot;-h&quot;/&gt;
  258. &lt;jvmarg value=&quot;-Xrunhprof:cpu=samples,file=log.txt,depth=3&quot;/&gt;
  259. &lt;/java&gt;
  260. </pre>
  261. <strong>Note</strong>: you can not specify the (highly deprecated) MSJVM, "jview.exe" as the
  262. JVM, as it takes different parameters for other JVMs,
  263. That JVM can be started from &lt;exec&gt; if required.
  264. <hr>
  265. <p align="center">Copyright &copy; 2000-2003 Apache Software Foundation. All rights
  266. Reserved.</p>
  267. </body>
  268. </html>