Coding With Fun
Home Docker Django Node.js Articles FAQ

Ant executes Java code

May 25, 2021 Apache Ant

Table of contents

Ant executes Java code

You can use Ant to execute Java code. In the following example, the java class file given requires a parameter (the administrator's email address) and an email is sent after execution.

public class NotifyAdministrator
   public static void main(String[] args)
      String email = args[0];
      System.out.println("Administrator "+email+" has been notified");
   public static void notifyAdministratorviaEmail(String email

Here's a build file for the build you need .xml java class file above.

<?xml version="1.0"?>
<project name="sample" basedir="." default="notify">
   <target name="notify">
      <java fork="true" failonerror="yes" classname="NotifyAdministrator">
         <arg line="[email protected]"/>

When the build .xml is executed, the following output is generated:

Buildfile: C:\build.xml

notify: [java] Administrator has been notified

Total time: 1 second

In the example here, the java code does a simple thing -- send an e-mail message. W e can also use the Ant task to do this. However, now that you have this idea, you can extend your build.xml file to call java code and do the same thing, such as: you can also encrypt your code.