1

I have a class that imports some servlet libraries. When I compile it from command-line it is fine.

When I use the ant compile task to compile it, it gives the errors that it can't find the servlet libraries in its path.

Is that a known/common occurrence?

Here is my Ant target:

<target name="compile" depends="prepare" description="compile the source" >
    <echo>=== COMPILE === SRCDIR: ${src}/com/udfr/src/java </echo> <!-- Compile the java code from ${src} into ${build} -->
    <javac srcdir="${src}/com/udfr/src/java" destdir="${dist}/WEB-INF/classes"/>
</target>
Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
Genadinik
  • 18,153
  • 63
  • 185
  • 284

2 Answers2

2

It's a common occurrence if you don't specify the servlet libraries properly in the classpath for the javac task... I suspect that's the problem. If you post the task which fails and the command line which works, we'll be able to help more.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

For some reason, the JAR file containing the Servlet API is part of your classpath when you compile your program in command line. However, it's not in the classpath of the javac Ant task.

You should explicitely add the JAR file to the classpath in your javac Ant task. There are several ways to do that; please read http://ant.apache.org/manual/Tasks/javac.html

Laurent Pireyn
  • 6,735
  • 1
  • 29
  • 39
  • Actually, the funny thing is that my CLASSPATH is set. I mean, Javac works. Just not from the ant task which I posted after Jon's answer. – Genadinik Apr 11 '11 at 19:23
  • @Genadinik I'm pretty sure the `CLASSPATH` environment variable is ignored by the `javac` Ant task, so that would explain why the command line works and the Ant task doesn't. Please try specifying the `classpath` attribute on the `javac` Ant task. – Laurent Pireyn Apr 11 '11 at 19:27
  • I'm not sure about that - my guess is that CLASSPATH is set for a specific process, whereas the process running ANT (e.g. Eclipse) is not aware of the CLASSPATH. Try setting the classpath in the Javac task itself. – RonK Apr 11 '11 at 19:36
  • Yes funny enough, adding this worked: classpath="/usr/local/tomcat/lib/*:/usr/local/jdk1.6.0_24/jre/lib/ext/servlet.jar" but adding just this didn't: classpath="/usr/local/tomcat/lib/*" -- any idea why the more elegant solution would not work here? – Genadinik Apr 11 '11 at 19:41
  • 1
    @Genadinik You should read about the path structures in Ant: http://ant.apache.org/manual/using.html#path – Laurent Pireyn Apr 11 '11 at 19:43