source: other-projects/trunk/realistic-books/packages/AntInstaller/web/manual1.6.2/manual/CoreTasks/java.html@ 19253

Last change on this file since 19253 was 19253, checked in by davidb, 15 years ago

Establishing a source code repository for Veronica's Realistic Book's software

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