# Java Task

## 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 write the output to. | No
newenvironment | Do not propagate old environment when new environment variables are specified. Default is "false" (ignored if fork is disabled). | No

### Parameters specified as nested elements

#### arg and jvmarg

Use nested `<arg>` and `<jvmarg>` elements to specify arguments for the or the forked VM. 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 © 2001-2002 Apache Software Foundation. All rights Reserved.