How do I compile my project using Ant?
Author: Deron Eriksson
Description: This Ant tutorial describes how to compile Java classes using the javac task and filesets.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.1 || Tomcat 5.5.20


Page:    1 2 >

The AntSW 'javac' task makes compiling classes in a project in EclipseSW very easy. In this short tutorial, we'll utilize the following 'tomcat-demo' project. This is a simple project that features a servletW. This project runs in TomcatSW, so it requires some of the jarW files that come with Tomcat in order to be compiled. The servlet utilizes the commons-lang library, so Ant needs to 'know' about this library when it compiles our JavaSW classes for us. The project also features a test.properties file that we'll copy from the src to the bin directory.

tomcat-demo project

Out simple TestServlet class is as follows:

TestServlet.java

package test;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

public class TestServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("This is the Test Servlet<br/>");
		out.println(StringUtils.swapCase("Howdy how are you"));
	}
}

The output of this trivial servlet is:

This is the Test Servlet
hOWDY HOW ARE YOU

Let's go ahead and jump right in. To start off, let's create a 'compile' target that utilizes the javac task to try to compile the javaSW files in the src directory (via 'srcdir') and puts the compiled class files in the bin directory (via 'destdir'):


	<target name="compile">
		<javac srcdir="src" destdir="bin"/>
	</target>

If we execute this task, we receive the following result:

Buildfile: C:\projects\workspace\tomcat-demo\build.xml
compile:
    [javac] Compiling 1 source file to C:\projects\workspace\tomcat-demo\bin
    [javac] C:\projects\workspace\tomcat-demo\src\test\TestServlet.java:6: package javax.servlet.http does not exist
    [javac] import javax.servlet.http.HttpServlet;
    [javac] ^
    [javac] C:\projects\workspace\tomcat-demo\src\test\TestServlet.java:7: package javax.servlet.http does not exist
    [javac] import javax.servlet.http.HttpServletRequest;
    [javac] ^
    [javac] C:\projects\workspace\tomcat-demo\src\test\TestServlet.java:8: package javax.servlet.http does not exist
    [javac] import javax.servlet.http.HttpServletResponse;
    [javac] ^
    [javac] C:\projects\workspace\tomcat-demo\src\test\TestServlet.java:10: package org.apache.commons.lang does not exist
    [javac] import org.apache.commons.lang.StringUtils;
    [javac] ^
    [javac] C:\projects\workspace\tomcat-demo\src\test\TestServlet.java:12: cannot find symbol
    [javac] symbol: class HttpServlet
    [javac] public class TestServlet extends HttpServlet {
    [javac] ^
    [javac] C:\projects\workspace\tomcat-demo\src\test\TestServlet.java:16: cannot find symbol
    [javac] symbol  : class HttpServletRequest
    [javac] location: class test.TestServlet
    [javac] protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    [javac] ^
    [javac] C:\projects\workspace\tomcat-demo\src\test\TestServlet.java:16: cannot find symbol
    [javac] symbol  : class HttpServletResponse
    [javac] location: class test.TestServlet
    [javac] protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    [javac] ^
    [javac] C:\projects\workspace\tomcat-demo\src\test\TestServlet.java:20: cannot find symbol
    [javac] symbol  : variable StringUtils
    [javac] location: class test.TestServlet
    [javac] out.println(StringUtils.swapCase("Howdy how are you"));
    [javac] ^
    [javac] 8 errors

BUILD FAILED
C:\projects\workspace\tomcat-demo\build.xml:18: Compile failed; see the compiler error output for details.

Total time: 1 second

So, what went wrong? The problem is that Ant can't find certain classes (like HttpServlet) that it needs in order to be able to compile the TestServlet class successfully. Classes such as HttpServlet are located in our Tomcat installation, and StringUtils (the last class that couldn't be found) is located in the project's WEB-INF/lib directory.

(Continued on page 2)

Page:    1 2 >