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

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