## Java

### Description

Executes a Java class within the running (Ant) VM or forks another VM if specified.

If odd things go wrong when you run this task, set fork="true" to use a new JVM.

### Parameters

Attribute | Description | Required
classname | the Java class to execute. | Either jar or classname
jar | the location of the jar file to execute (must have a Main-Class entry in the manifest). Fork must be set to true if this option is selected. | Either jar or classname
args | the arguments for the class that is executed. deprecated, use nested `<arg>` elements instead. | No
classpath | the classpath to use. | No
classpathref | the classpath to use, given as reference to a PATH defined elsewhere. | No
fork | if enabled triggers the class execution in another VM (disabled by default) | No
jvm | the command used to invoke the Java Virtual Machine, default is 'java'. The command is resolved by java.lang.Runtime.exec(). Ignored if fork is disabled. | No
jvmargs | the arguments to pass to the forked VM (ignored if fork is disabled). deprecated, use nested `<jvmarg>` elements instead. | No
maxmemory | Max amount of memory to allocate to the forked VM (ignored if fork is disabled) | No
failonerror | Stop the buildprocess if the command exits with a returncode other than 0. Default is "false" | No
dir | The directory to invoke the VM in. (ignored if fork is disabled) | No
output | Name of a file to which to write the output. If the error stream is not also redirected to a file or property, it will appear in this output. | No
error | The file to which the standard error of the command should be redirected. | No
logError | This attribute is used when you wish to see error output in Ant's log and you are redirecting output to a file/property. The error output will not be included in the output file/property. If you redirect error with the "error" or "errorProperty" attributes, this will have no effect. | No
append | Whether output and error files should be appended to or overwritten. Defaults to false. | No
outputproperty | The name of a property in which the output of the command should be stored. Unless the error stream is redirected to a separate file or stream, this property will include the error output. | No
errorproperty | The name of a property in which the standard error of the command should be stored. | No
input | A file from which the executed command's standard input is taken. This attribute is mutually exclusive with the inputstring attribute | No
inputstring | A string which serves as the input stream for the executed command. This attribute is mutually exclusive with the input attribute. | No
newenvironment | Do not propagate old environment when new environment variables are specified. Default is "false" (ignored if fork is disabled). | No
timeout | Stop the command if it doesn't finish within the specified time (given in milliseconds). It is highly recommended to use this feature only if fork is enabled. | No

### Parameters specified as nested elements

#### arg and jvmarg

Use nested `<arg>` and `<jvmarg>` elements to specify arguments for the Java class and the forked VM respectively. See Command line arguments.

#### sysproperty

Use nested `<sysproperty>` elements to specify system properties required by the class. These properties will be made available to the VM during the execution of the class (either ANT's VM or the forked VM). The attributes for this element are the same as for environment variables.

#### classpath

`Java`'s classpath attribute is a PATH like structure and can also be set via a nested classpath element.

#### env

It is possible to specify environment variables to pass to the forked VM via nested env elements. See the description in the section about exec

Settings will be ignored if fork is disabled.

### Examples

           <java classname="test.Main">
             <arg value="-h"/>
             <classpath>
               <pathelement location="dist/test.jar"/>
               <pathelement path="${java.class.path}"/>
             </classpath>
           </java>

Run a class in this JVM with a new jar on the classpath

            <java jar="dist/test.jar"
               fork="true"
               failonerror="true"
               maxmemory="128m"
               >
             <arg value="-h"/>
             <classpath>
               <pathelement location="dist/test.jar"/>
               <pathelement path="${java.class.path}"/>
             </classpath>
           </java>

Run the jar using the manifest supplied entry point, forking (as required), and with a maximum memory of 128MB. Any non zero return code breaks the build.

      <java classname="test.Main"/>

      <java classname="test.Main"
            fork="yes" >
        <sysproperty key="DEBUG" value="true"/>
        <arg value="-h"/>
        <jvmarg value="-Xrunhprof:cpu=samples,file=log.txt,depth=3"/>
      </java>

Note: you can not specify the (highly deprecated) MSJVM, "jview.exe" as the JVM, as it takes different parameters for other JVMs, That JVM can be started from <exec> if required.

Copyright © 2000-2002 Apache Software Foundation. All rights Reserved.