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