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];
      notifyAdministratorviaEmail(email);
      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]"/>
      </java>
   </target>
</project>

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

C:\>ant
Buildfile: C:\build.xml

notify: [java] Administrator admin@test.com has been notified

BUILD SUCCESSFUL
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.