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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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>Java Task</title>
  20. </head>
  21. <body>
  22. <h2><a name="java">Java</a></h2>
  23. <h3>Description</h3>
  24. <p>Executes a Java class within the running (Apache Ant) VM or forks another VM if
  25. specified.</p>
  26. <p>
  27. If odd things go wrong when you run this task, set fork="true" to use a new
  28. JVM.
  29. <p>As of Ant 1.6.3, you can interact with a forked VM, as well as
  30. sending input to it via the <code>input</code> and <code>inputstring</code>
  31. attributes.</p>
  32. <h4><a name="background">Running Ant as a background process on
  33. Unix(-like) systems</a></h4>
  34. <p>If you run Ant as a background process (like <code>ant &</code>)
  35. and use the <code>&lt;java&gt;</code> task with <code>spawn</code>
  36. set to <code>false</code> and <code>fork</code>
  37. to <code>true</code>, you must provide explicit input to the forked
  38. process or Ant will be suspended because it tries to read from the
  39. standard input.</p>
  40. <h3>Parameters</h3>
  41. <table border="1" cellpadding="2" cellspacing="0">
  42. <tr>
  43. <td valign="top"><b>Attribute</b></td>
  44. <td valign="top"><b>Description</b></td>
  45. <td align="center" valign="top"><b>Required</b></td>
  46. </tr>
  47. <tr>
  48. <td valign="top"><a name="classname">classname</a></td>
  49. <td valign="top">the Java class to execute.</td>
  50. <td align="center" valign="top">Either <tt>jar</tt>, <tt>classname</tt> or <tt>module</tt></td>
  51. </tr>
  52. <tr>
  53. <td valign="top">jar</td>
  54. <td valign="top">the location of the jar file to execute (must have a
  55. Main-Class entry in the manifest). Fork must be set to true if this option is selected.
  56. See notes below for more details.
  57. </td>
  58. <td align="center" valign="top">Either <tt>jar</tt>, <tt>classname</tt> or <tt>module</tt></td>
  59. </tr>
  60. <tr>
  61. <td valign="top">args</td>
  62. <td valign="top">the arguments for the class that is
  63. executed. <b>deprecated, use nested <code>&lt;arg&gt;</code>
  64. elements instead.</b></td>
  65. <td align="center" valign="top">No</td>
  66. </tr>
  67. <tr>
  68. <td valign="top">classpath</td>
  69. <td valign="top">the classpath to use.</td>
  70. <td align="center" valign="top">No</td>
  71. </tr>
  72. <tr>
  73. <td valign="top">classpathref</td>
  74. <td valign="top">the classpath to use, given as <a
  75. href="../using.html#references">reference</a> to a PATH defined elsewhere.</td>
  76. <td align="center" valign="top">No</td>
  77. </tr>
  78. <tr>
  79. <td valign="top">fork</td>
  80. <td valign="top">if enabled triggers the class execution in another VM
  81. (disabled by default)</td>
  82. <td align="center" valign="top">No</td>
  83. </tr>
  84. <tr>
  85. <td valign="top">spawn</td>
  86. <td valign="top">if enabled allows to start a process which will outlive ant.<br>
  87. Requires fork=true, and not compatible
  88. with timeout, input, output, error, result attributes.<br>
  89. (disabled by default)</td>
  90. <td align="center" valign="top">No</td>
  91. </tr>
  92. <tr>
  93. <td valign="top">jvm</td>
  94. <td valign="top">the command used to invoke the Java Virtual Machine,
  95. default is 'java'. The command is resolved by java.lang.Runtime.exec().
  96. Ignored if fork is disabled.
  97. </td>
  98. <td align="center" valign="top">No</td>
  99. </tr>
  100. <tr>
  101. <td valign="top">jvmargs</td>
  102. <td valign="top">the arguments to pass to the forked VM (ignored
  103. if fork is disabled). <b>deprecated, use nested
  104. <code>&lt;jvmarg&gt;</code> elements instead.</b></td>
  105. <td align="center" valign="top">No</td>
  106. </tr>
  107. <tr>
  108. <td valign="top">maxmemory</td>
  109. <td valign="top">Max amount of memory to allocate to the forked VM
  110. (ignored if fork is disabled)</td>
  111. <td align="center" valign="top">No</td>
  112. </tr>
  113. <tr>
  114. <td valign="top">module</td>
  115. <td valign="top">The initial or main module to resolve. To specify
  116. the module main class use the <a href="#classname">classname</a> attribute.
  117. Fork must be set to true if this option is selected.<em>since Ant 1.9.7</em></td>
  118. <td align="center" valign="top">Either <tt>jar</tt>, <tt>classname</tt> or <tt>module</tt></td>
  119. </tr>
  120. <tr>
  121. <td valign="top">modulepath</td>
  122. <td valign="top">Specify where to find application modules. A list of directories of modules, module files or exploded modules.<em>since Ant 1.9.7</em></td>
  123. <td align="center" valign="top">No</td>
  124. </tr>
  125. <tr>
  126. <td valign="top">modulepathref</td>
  127. <td valign="top">The modulepath to use, given as <a
  128. href="../using.html#references">reference</a> to a PATH defined elsewhere.
  129. <em>since Ant 1.9.7</em></td>
  130. <td align="center" valign="top">No</td>
  131. </tr>
  132. <tr>
  133. <td valign="top">failonerror</td>
  134. <td valign="top">Stop the buildprocess if the command exits with a
  135. returncode other than 0. Default is "false" (see <a href="#failonerror">note</a>)</td>
  136. <td align="center" valign="top">No</td>
  137. </tr>
  138. <tr>
  139. <td valign="top">resultproperty</td>
  140. <td valign="top">The name of a property in which the return code of the
  141. command should be stored. Only of interest if failonerror=false
  142. and if fork=true.</td>
  143. <td align="center" valign="top">No</td>
  144. </tr>
  145. <tr>
  146. <td valign="top">dir</td>
  147. <td valign="top">The directory to invoke the VM in. (ignored if
  148. fork is disabled)</td>
  149. <td align="center" valign="top">No</td>
  150. </tr>
  151. <tr>
  152. <td valign="top">output</td>
  153. <td valign="top">Name of a file to which to write the output. If the error stream
  154. is not also redirected to a file or property, it will appear in this output.</td>
  155. <td align="center" valign="top">No</td>
  156. </tr>
  157. <tr>
  158. <td valign="top">error</td>
  159. <td valign="top">The file to which the standard error of the command should be
  160. redirected. </td>
  161. <td align="center" valign="top">No</td>
  162. </tr>
  163. <tr>
  164. <td valign="top">logError</td>
  165. <td valign="top">This attribute is used when you wish to see error output in Ant's
  166. log and you are redirecting output to a file/property. The error
  167. output will not be included in the output file/property. If you
  168. redirect error with the &quot;error&quot; or &quot;errorProperty&quot;
  169. attributes, this will have no effect.</td>
  170. <td align="center" valign="top">No</td>
  171. </tr>
  172. <tr>
  173. <td valign="top">append</td>
  174. <td valign="top">Whether output and error files should be appended to or overwritten.
  175. Defaults to false.</td>
  176. <td align="center" valign="top">No</td>
  177. </tr>
  178. <tr>
  179. <td valign="top">outputproperty</td>
  180. <td valign="top">The name of a property in which the output of the
  181. command should be stored. Unless the error stream is redirected to a separate
  182. file or stream, this property will include the error output.</td>
  183. <td align="center" valign="top">No</td>
  184. </tr>
  185. <tr>
  186. <td valign="top">errorproperty</td>
  187. <td valign="top">The name of a property in which the standard error of the
  188. command should be stored.</td>
  189. <td align="center" valign="top">No</td>
  190. </tr>
  191. <tr>
  192. <td valign="top">input</td>
  193. <td valign="top">A file from which the executed command's standard input
  194. is taken. This attribute is mutually exclusive with the
  195. inputstring attribute</td>
  196. <td align="center" valign="top">No; default is to take standard input from console
  197. (unless <code>spawn="true"</code>)</td>
  198. </tr>
  199. <tr>
  200. <td valign="top">inputstring</td>
  201. <td valign="top">A string which serves as the input stream for the
  202. executed command. This attribute is mutually exclusive with the
  203. input attribute.</td>
  204. <td align="center" valign="top">No; default is to take standard input from console
  205. (unless <code>spawn="true"</code>)</td>
  206. </tr>
  207. <tr>
  208. <td valign="top">newenvironment</td>
  209. <td valign="top">Do not propagate old environment when new
  210. environment variables are specified. Default is &quot;false&quot;
  211. (ignored if fork is disabled).</td>
  212. <td align="center" valign="top">No</td>
  213. </tr>
  214. <tr>
  215. <td valign="top">timeout</td>
  216. <td valign="top">Stop the command if it doesn't finish within the
  217. specified time (given in milliseconds). <strong>It is highly
  218. recommended to use this feature only if fork is enabled.</strong></td>
  219. <td align="center" valign="top">No</td>
  220. </tr>
  221. <tr>
  222. <td valign="top">clonevm</td>
  223. <td valign="top">If set to true, then all system properties
  224. and the bootclasspath of the forked Java Virtual Machine will be
  225. the same as those of the Java VM running Ant. Default is
  226. &quot;false&quot; (ignored if fork is disabled).
  227. <em>since Ant 1.7</em></td>
  228. <td align="center" valign="top">No</td>
  229. </tr>
  230. </table>
  231. <h3>Parameters specified as nested elements</h3>
  232. <h4>arg and jvmarg</h4>
  233. <p>Use nested <code>&lt;arg&gt;</code> and <code>&lt;jvmarg&gt;</code>
  234. elements to specify arguments for the Java class and the forked VM respectively.
  235. See <a href="../using.html#arg">Command line arguments</a>.</p>
  236. <h4>sysproperty</h4>
  237. <p>Use nested <code>&lt;sysproperty&gt;</code>
  238. elements to specify system properties required by the class.
  239. These properties will be made available to the VM during the execution
  240. of the class (either ANT's VM or the forked VM). The attributes
  241. for this element are the same as for <a href="exec.html#env">environment
  242. variables</a>.</p>
  243. <h4>syspropertyset</h4>
  244. <p>You can specify a set of properties to be used as system properties
  245. with <a href="../Types/propertyset.html">syspropertyset</a>s.</p>
  246. <p><em>since Ant 1.6</em>.</p>
  247. <h4>classpath</h4>
  248. <p><code>Java</code>'s <i>classpath</i> attribute is a <a
  249. href="../using.html#path">PATH like structure</a> and can also be set via a nested
  250. <i>classpath</i> element.</p>
  251. <h4>bootclasspath</h4>
  252. <p>The location of bootstrap class files can be specified using this
  253. <a href="../using.html#path">PATH like structure</a> - will be ignored
  254. if <i>fork</i> is not <code>true</code> or the target VM doesn't
  255. support it (i.e. Java 1.1).</p>
  256. <p><em>since Ant 1.6</em>.</p>
  257. <h4>env</h4>
  258. <p>It is possible to specify environment variables to pass to the
  259. forked VM via nested <i>env</i> elements. See the description in the
  260. section about <a href="exec.html#env">exec</a></p>
  261. <p>Settings will be ignored if fork is disabled.</p>
  262. <h4>permissions</h4>
  263. <p>Security permissions can be revoked and granted during the execution of the
  264. class via a nested <i>permissions</i> element. For more information please
  265. see <a href="../Types/permissions.html">permissions</a></p>
  266. <p>When the permission RuntimePermission exitVM has not been granted (or has
  267. been revoked) the System.exit() call will be intercepted
  268. and treated like indicated in <i>failonerror</i>.</p>
  269. <p>Note:<br>
  270. If you do not specify permissions,
  271. a set of default permissions will be added to your Java invocation to make
  272. sure that the ant run will continue or terminated as indicated by
  273. <i>failonerror</i>. All permissions not granted per default will be
  274. checked by whatever security manager was already in place. exitVM will be
  275. disallowed.
  276. </p>
  277. <p>Settings will be ignored if fork is enabled.</p>
  278. <p><em>since Ant 1.6</em>.</p>
  279. <h4>assertions</h4>
  280. <p>You can control enablement of Java 1.4 assertions with an
  281. <a href="../Types/assertions.html"><tt>&lt;assertions&gt;</tt></a>
  282. subelement.</p>
  283. <p>Assertion statements are currently ignored in non-forked mode.</p>
  284. <p><em>since Ant 1.6.</em></p>
  285. <a name="redirector"><h4>redirector</h4></a>
  286. <i><b>Since Ant 1.6.2</b></i>
  287. <p>A nested <a href="../Types/redirector.html">I/O Redirector</a>
  288. can be specified. In general, the attributes of the redirector behave
  289. as the corresponding attributes available at the task level. The most
  290. notable peculiarity stems from the retention of the <code>&lt;java&gt;</code>
  291. attributes for backwards compatibility. Any file mapping is done
  292. using a <CODE>null</CODE> sourcefile; therefore not all
  293. <a href="../Types/mapper.html">Mapper</a> types will return
  294. results. When no results are returned, redirection specifications
  295. will fall back to the task level attributes. In practice this means that
  296. defaults can be specified for input, output, and error output files.
  297. </p>
  298. <a name="failonerror"><h3>Errors and return codes</h3></a>
  299. By default the return code of a <code>&lt;java&gt;</code> is ignored.
  300. Alternatively, you can set <code>resultproperty</code> to the name
  301. of a property and have it assigned to the result code (barring immutability,
  302. of course).
  303. When you set <code>failonerror="true"</code>, the only possible value for
  304. <code>resultproperty</code> is 0. Any non-zero response is treated as an
  305. error and would mean the build exits.
  306. <p> Similarly, if <code>failonerror="false"</code> and <code>fork="false"</code>
  307. , then <code>&lt;java&gt;</code> <b>must</b> return 0 otherwise the build will
  308. exit, as the class was run by the build JVM.</p>
  309. <a name="modulepath"><h4>modulepath</h4>
  310. <i><b>Since Ant 1.9.7</b></i>
  311. <p><code>Java</code>'s <i>modulepath</i> attribute is a <a
  312. href="../using.html#path">PATH like structure</a> and can also be set via a nested
  313. <i>modulepath</i> element.</p>
  314. <a name="upgrademodulepath"><h4>upgrademodulepath</h4>
  315. <i><b>Since Ant 1.9.7</b></i>
  316. <p>The location of modules that replace upgradeable modules in the runtime image
  317. can be specified using this <a href="../using.html#path">PATH like structure</a>.</p>
  318. <h3>JAR file execution</h3>
  319. <p>The parameter of the <tt>jar</tt> attribute is of type <tt>File</tt>;
  320. that is, the parameter is resolved to an absolute file relative to the
  321. base directory of the project, <i>not</i> the directory in which the Java
  322. task is run. If you need to locate a JAR file relative to the directory
  323. the task will be run in, you need to explicitly create the full path
  324. to the JAR file.</p>
  325. <p>When using the <tt>jar</tt> attribute, all classpath settings are
  326. ignored according to <a href="http://docs.oracle.com/javase/7/docs/technotes/tools/windows/java.html">Oracle's
  327. specification</a>.
  328. <h3>Examples</h3>
  329. <pre>
  330. &lt;java classname=&quot;test.Main&quot;&gt;
  331. &lt;arg value=&quot;-h&quot;/&gt;
  332. &lt;classpath&gt;
  333. &lt;pathelement location=&quot;dist/test.jar&quot;/&gt;
  334. &lt;pathelement path=&quot;${java.class.path}&quot;/&gt;
  335. &lt;/classpath&gt;
  336. &lt;/java&gt;
  337. </pre>
  338. Run a class in this JVM with a new jar on the classpath
  339. <pre>
  340. &lt;java jar=&quot;dist/test.jar&quot;
  341. fork="true"
  342. failonerror="true"
  343. maxmemory="128m"
  344. &gt;
  345. &lt;arg value=&quot;-h&quot;/&gt;
  346. &lt;classpath&gt;
  347. &lt;pathelement location=&quot;dist/test.jar&quot;/&gt;
  348. &lt;pathelement path=&quot;${java.class.path}&quot;/&gt;
  349. &lt;/classpath&gt;
  350. &lt;/java&gt;
  351. </pre>
  352. Run the JAR test.jar in this project's dist/lib directory.
  353. using the manifest supplied entry point, forking (as required),
  354. and with a maximum memory of 128MB. Any non zero return code breaks the build.
  355. <pre>
  356. &lt;java
  357. dir="${exec.dir}"
  358. jar=&quot;${exec.dir}/dist/test.jar&quot;
  359. fork="true"
  360. failonerror="true"
  361. maxmemory="128m"
  362. &gt;
  363. &lt;arg value=&quot;-h&quot;/&gt;
  364. &lt;classpath&gt;
  365. &lt;pathelement location=&quot;dist/test.jar&quot;/&gt;
  366. &lt;pathelement path=&quot;${java.class.path}&quot;/&gt;
  367. &lt;/classpath&gt;
  368. &lt;/java&gt;
  369. </pre>
  370. Run the JAR dist/test.jar relative to the directory
  371. <tt>${exec.dir}</tt>, this being the same directory into which the JVM
  372. is to start up.
  373. <pre> &lt;java classname=&quot;test.Main&quot;/&gt;</pre>
  374. Runs a given class with the current classpath.
  375. <pre>
  376. &lt;java classname=&quot;test.Main&quot;
  377. fork=&quot;yes&quot; &gt;
  378. &lt;sysproperty key=&quot;DEBUG&quot; value=&quot;true&quot;/&gt;
  379. &lt;arg value=&quot;-h&quot;/&gt;
  380. &lt;jvmarg value=&quot;-Xrunhprof:cpu=samples,file=log.txt,depth=3&quot;/&gt;
  381. &lt;/java&gt;
  382. </pre>
  383. Add system properties and JVM-properties to the JVM as in
  384. <code>java ="-Xrunhprof:cpu=samples,file=log.txt,depth=3 -DDEBUG=true test.Main</code>
  385. <pre> &lt;java classname=&quot;ShowJavaVersion&quot; classpath=&quot;.&quot;
  386. jvm=&quot;path-to-java14-home/bin/java&quot; fork=&quot;true&quot;
  387. taskname=&quot;java1.4&quot; &gt;
  388. </pre>
  389. Use a given Java implementation (another the one Ant is currently using) to run the class.
  390. For documentation in the log <code>taskname</code> is used to change the <code>[java]</code>
  391. log-prefix to <code>[java1.4]</code>.
  392. <p><strong>Note</strong>: you can not specify the (highly deprecated) MSJVM, "jview.exe" as the
  393. JVM, as it takes different parameters for other JVMs,
  394. That JVM can be started from <code>&lt;exec&gt;</code> if required.</p>
  395. <pre>
  396. &lt;java
  397. fork=&quot;true&quot;
  398. failonerror=&quot;true&quot;
  399. maxmemory=&quot;128m&quot;
  400. module=&quot;TestModule&quot;
  401. modulepath=&quot;lib:dist/test.jar&quot;/&gt;
  402. </pre>
  403. Runs the module TestModule resolved on the modulepath <tt>lib/:dist/test.jar</tt>
  404. with a maximum memory of 128MB. Any non zero return code breaks the build.
  405. <pre>
  406. &lt;java
  407. fork=&quot;true&quot;
  408. failonerror=&quot;true&quot;
  409. maxmemory=&quot;128m&quot;
  410. module=&quot;TestModule&quot;
  411. classname=&quot;Main&quot;&gt;
  412. &lt;modulepath&gt;
  413. &lt;pathelement location=&quot;lib&quot;/&gt;
  414. &lt;pathelement location=&quot;dist/test.jar&quot;/&gt;
  415. &lt;/modulepath&gt;
  416. &lt;/java&gt;
  417. </pre>
  418. Runs the class Main in module TestModule resolved on the modulepath <tt>lib/:dist/test.jar</tt>
  419. with a maximum memory of 128MB. Any non zero return code breaks the build.
  420. </body>
  421. </html>