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