Apache Ant Tasks - Java


Description

Java task executes a Java class within the running JVM or forks another JVM if specified using fork=true;

Properties

Sr.No Attributes & Description
1

Classname

The Java class to execute.

2

Jar

The location of the jar file to execute. fork must be set to true if this option is selected.

3

Module

The initial or main module to resolve (must have a Main-Class entry in the manifest). fork must be set to true if this option is selected.

4

Sourcefile

The location of a ".java" file or a file containing shebang with Java source code. Set this attribute to run Java single file source programs, a feature introduced in Java 11. fork must be set to true if this option is selected.

5

Classpath

The classpath to use.

6

Classpathref

The classpath to use, given as reference to a Path defined elsewhere.

7

Modulepath

Specify where to find application modules. A list of directories of modules, module files or exploded modules.

8

modulepathref

The modulepath to use, given as reference to a Path defined elsewhere.

9

Fork

If enabled triggers the class execution in another JVM.

10

Spawn

If enabled allows to start a process which will outlive Ant. Requires that fork is true, and not compatible with timeout, input, output, error, result attributes

11

Sourcefile

The location of a ".java" file or a file containing shebang with Java source code. Set this attribute to run Java single file source programs, a feature introduced in Java 11. fork must be set to true if this option is selected.

12

jvm

The command used to invoke JVM. The command is resolved by java.lang.Runtime.exec(). Ignored if fork is false.

13

Maxmemory

Max amount of memory to allocate to the forked JVM, ignored if fork is false.

14

Failonerror

Stop the build process if the command exits with a return code other than 0.

15

resultproperty

The name of a property in which the return code of the command should be stored. Only of interest if failonerror is false and if fork is true.

16

DIR

The directory to invoke the JVM in, ignored if fork is false.

17

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.

18

Error

The file to which the standard error of the command should be redirected.

19

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.

20

Append

Whether output and error files should be appended to or overwritten.

21

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.

22

Errorproperty

The name of a property in which the standard error of the command should be stored.

23

Input

A file from which the executed command's standard input is taken. This attribute is mutually exclusive with the input string attribute.

24

Inputstring

A string which serves as the input stream for the executed command. This attribute is mutually exclusive with the input attribute.

25

Newenvironment

Do not propagate old environment when new environment variables are specified.

26

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 true.

27

Clonevm

If set to true, then all system properties and the bootclasspath of the forked JVM will be the same as those of the JVM running Ant.

28

Discardoutput

Whether output should completely be discarded. This setting is incompatible with any setting that redirects output to files or properties. If you set this to true error output will be discared as well unless you redirect error output to files, properties or enable logError.

29

Discarderror

Whether error output should completely be discarded. This setting is incompatible with any setting that redirects error output to files or properties as well as logError.

Example

Usage

Create TestMessage.java with the following content −

public class TestMessage {
   public static void main(String[] args) {
      System.out.println("Welcome to tutorialspoint.com");
   }
}

Create build.xml with the following content −

<?xml version="1.0"?>
<project name="TutorialPoint" default="info">
   <target name="info">      
      <java classname="TestMessage" classpath="."/>
   </target>
</project>

Above script will run a java class file to print output.

Output

Running Ant on the above build file produces the following output −

F:\tutorialspoint\ant>ant
Buildfile: F:\tutorialspoint\ant\build.xml

info:
   [java] Welcome to tutorialspoint.com

BUILD SUCCESSFUL
Total time: 0 seconds
Advertisements