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

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