File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Cattle Drive and the fly likes ANT does not use %CATALINA_HOME% to find servlet-api.jar on Windows 7 64 bit and build fails Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "ANT does not use %CATALINA_HOME% to find servlet-api.jar on Windows 7 64 bit and build fails" Watch "ANT does not use %CATALINA_HOME% to find servlet-api.jar on Windows 7 64 bit and build fails" New topic
Forums: Cattle Drive Ant, Maven and Other Build Tools
Author

ANT does not use %CATALINA_HOME% to find servlet-api.jar on Windows 7 64 bit and build fails

margaret gillon
Ranch Hand

Joined: Nov 12, 2008
Posts: 318
    
    5

I am working on a project in the Cattle Drive and building servlets. The instructions and build.xml work fine on my Windows XP3 machine at work but at home I have to manually point to the tomcat directory when compiling with javac and when running an ANT file for the build the build fails because ANT doesn't go to tomcat and find the servlet-api.jar.
The servlet is in package com.javaranch.drive ; so the source code folder is C:\java\ReverseServlet\src\java\com\javaranch\drive\

I checked the environmental variables in the command window with the echo command and they do have values....


This is the lesson's instruction of the command to use to compile the servlet


This is what I have to use for the compile to work.


I cannot add the -d ../build/classes or the compile fails, seems Windows7 doesn't like going up and down the directory tree.

The compile with the hard coded directory works and I get a .class file. But ANT doesn't work because it cannot find the Tomcat libraries.

The build.xml



Here is the ANT output, remember I just compiled the same class with javac and hard coded the directory in the compile command and the class compiled without errors.

Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5823
    
    7

It appears that you are mixing Windows and Linux conventions (perhaps the exercises are geared towards Linux and you are expected to translate to Windows). Thus this:

javac -cp $CATALINA_HOME/lib/servlet-api.jar -d ../build/classes ReverseServlet.java

should really be

javac -cp %CATALINA_HOME%/lib/servlet-api.jar -d ../build/classes ReverseServlet.java

Also, when you run that command, the ../build/classes directory must exist - javac will not create the directory if it is missing, it will only complain and fail to run. (Neither Windows 7 nor 8 has ever given me problems with such a relative path.)

As far as the build.xml not working, the compiler classpath mentioned at lines 20-23 doesn't include any of the Tomcat libraries. Were you, perhaps supposed to copy the to you project's lib directory?



JBoss In Action
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2848
    
  11

Hi Margaret,

First, for the command line, it simply looks like you're using the Unix/Linux/Mac version of the compile command. Your example shows:
javac -cp $CATALINA_HOME/lib/servlet-api.jar -d ../build/classes ReverseServlet.java

In the Windows world, it should be:
javac -cp %CATALINA_HOME%\lib\servlet-api.jar -d ..\build\classes ReverseServlet.java

Hopefully, that's all that's wrong there. I don't know about Windows 7 not going up and down the directory, i.e., being able to resolve the "..". It seems to work for me. Maybe give that another try.

As for the Ant build, you've jumped a bit ahead of me. Try this build.xml instead:



Note that this assumes that the Java source code is actually under the directory src\java, and any web resources, like html files are under src\web.
margaret gillon
Ranch Hand

Joined: Nov 12, 2008
Posts: 318
    
    5

Where to start. Thanks Peter and Greg for answering.

Peter, you were correct about the syntax. I have instructions with commands for both linux and windows and I copied the linux syntax without noticing.

I am using the Windows % % now as I did when I used the echo tests. I added the com.javaranch.drive package to the servlet as part of the assignment. I have been editing the environmental variables and restarting the computer all day , including adding a JAVA_HOME and a CATALINA_HOME to the user environmental variables. Since my edits the %CATALINA_HOME% works but the -d flag is still throwing an error.

I double checked my computer and I do have the directory set up for the -d switch. I tried the directory paths with / and with \ and get the errror both ways.



Here is a cut and paste from the command window showing what happens.



margaret gillon
Ranch Hand

Joined: Nov 12, 2008
Posts: 318
    
    5

Hi Greg,

If I use the build.xml you posted I get this



The build.xml I was using is the one from the bee example in the revised Cattle Drive instructions. It worked on my XP box on Wednesday. I just changed the project name when I copied it to ReversedServlet.


Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2848
    
  11

OK, I see the problem with your -d parameter. The ".." is relative to the current directory. Now that you've added a java subdirectory to the structure, you have to take that into account when using relative paths. For example, to get from the ~/src/java directory to the ~/build/classes directory, you need to specify ../../build/classes. If you just use ../build/classes, you will rightly get an error message saying there is no such directory. Now, to complicate it even further, you are using packages. You've gone all the way down into the package hierarchy for the compile, which is fine since you only have one java file, but you still have to specify the output directory relative to where you are, so it's going to be a lot of "../"s. If I'm counting right, you'll need ../../../../../build/classes. At that point, it's probably easier just to type the absolute path, like -d c:/java/ReverseServlet/build/classes ... and yes, the forward slashes should work, even on Windows.

That's interesting with the build file. I'm not getting that error, but that's possibly because I'm using an older version of Ant to test with ... 1.8.2. I'll pull down something a little less moldy and see if I can reproduce that problem. Meanwhile, you could just delete the first line and see if that fixes it for you.
margaret gillon
Ranch Hand

Joined: Nov 12, 2008
Posts: 318
    
    5

Greg I tried your build.xml without the first line and it throws the same errors as the build.xml I originally posted.

you'll need ../../../../../build/classes


Yes, I suspected I would need the entire path because adding the package ( as instructed by my nitpicker ) extends the directories. That is one reason I was setting the ANT up, or compiling without the -d switch, and just moving the class around.

The reason I posted this in the ANT forum is that I was running the build.xml for a couple projects at work on a Windows XP machine with no problems. I started with the bee servlet exactly as it is posted on the Cattle Drive instructions and the bee.war built the first time. This is not the first time I have had problems on my Windows 7 machine. In March I was working on the building the model servlet for Bear's Frontman framework. I never got the servlet to compile on Netbeans, or from the command line, but Eclipse complied it. I was able to find posts on Stacktrace about people who had problems with ANT builds on Windows 7. Some of them traced the problem to older versions of java.exe in the systems directories such as \Windows\system32. Their experience was that the OS would find the old version in the system directory before it found the version in the JDK that had the rest of the Java libraries. These people removed the older java.exe's from the system directories and that solved the ANT problem. I have not tried testing this theory yet.



margaret gillon
Ranch Hand

Joined: Nov 12, 2008
Posts: 318
    
    5

Back on my Windows XP SP3 Pro 32-bit desktop this morning. Used Greg's build.xml as posted here Yesterday (Sunday) 03:31:40 PM . The ANT completed without errors on the first build.

Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2848
    
  11

Hmm, that's curious. You had an error on line 1 of the XML, then you deleted line 1, and you got the same error? That's making my head hurt just thinking about it.

The point about multiple versions of Java is a good one though. You should set a JAVA_HOME variable in your environment that points to the JDK you want Ant to use, and just to be extra sure make sure your PATH contains %JAVA_HOME%\bin ... make it first or at least earlier in the path than anything else that could contain a different Java version. I'll make sure that gets included in the instructions. You also want to confirm that you are using the same version of Ant on both platforms. ant -version should give you that info.
margaret gillon
Ranch Hand

Joined: Nov 12, 2008
Posts: 318
    
    5

Hi Greg,

I will try and clarify.

I deleted the first line in your build.xml and then ANT ran the build.xml you posted.... BUT ...... the resulting errors from your build.xml were the same as the original errors from my build.xml that I started the thread with. The full ANT error report is at the top of the thread. In other words neither of our build.xml worked on my Windows 7 machine because ANT did not find the javax.servlet

The ANT errors start with


Regarding the environmental variables...
You should set a JAVA_HOME variable in your environment that points to the JDK you want Ant to use, and just to be extra sure make sure your PATH contains %JAVA_HOME%\bin


These are set, I posted the results of testing for them with the "echo" command at the top of the thread. As an experiment yesterday I added the same variables to the user environmental variables in Windows 7. The Windows 7 user variables are distinct from the Wiindows 7 System environmental variables. As of last night and after more reboots, etc., the ANT build was still producing errors because it could not find the libraries.

Today both your build.xml and my build.xml created the ReverseServlet.war file without errors on my older Windows XP desktop.
margaret gillon
Ranch Hand

Joined: Nov 12, 2008
Posts: 318
    
    5

In his reply on 07/07/2013 03:23:11 PM Peter Johnson wrote:

As far as the build.xml not working, the compiler classpath mentioned at lines 20-23 doesn't include any of the Tomcat libraries. Were you, perhaps supposed to copy the to you project's lib directory?


No, I am not putting the tomcat servlet-api.jar in the project's lib directory. I have been told -- by Bear I think -- that is a bad practice. I would like the build to go to the tomcat directory. Since I am new to ANT can Peter, or another ANT user, explain how to put the classpath to tomcat install into the ANT build.xml as Peter suggested?

My tomcat directory is c:\tomcat\ the library is c:\tomcat\lib the jar is servlet-api.jar
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5823
    
    7

I would say to add this line after line 22 in your original build.xml:

margaret gillon
Ranch Hand

Joined: Nov 12, 2008
Posts: 318
    
    5



Peter, adding this line to the build.xml fixed the problem. ( Last night, on my Windows 7 Pro 64 bit machine, at home, that I could not previously get the build to complete on. )

Thank you
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2848
    
  11

margaret gillon wrote:


Peter, adding this line to the build.xml fixed the problem. Thank you


Good, but not a general solution, and so not really acceptable for the Cattle Drive. I'd really like to keep the build.xml generic for all users regardless of OS or configuration. Doing it this way also has the secondary problem that it includes every jar in the Tomcat lib into your classpath, not just the one you want. That will probably do no more harm than slowing down your compile a tiny bit, but you never know.

If you have time, could you try this build.xml:


Here's the output I get from running that, and then echoing the value of the environment variable:


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

test-env:
[echo] Catalina home: C:\opt\tomcat\apache-tomcat-7.0.34

BUILD SUCCESSFUL
Total time: 0 seconds

C:\Users\charlesg>echo %CATALINA_HOME%
C:\opt\tomcat\apache-tomcat-7.0.34


I suspect your output for the same tasks will be enough for us to figure out either where you've gone wrong or where the instructions are missing something.


margaret gillon
Ranch Hand

Joined: Nov 12, 2008
Posts: 318
    
    5

I will not be on Windows 7 until later tonight. I will post the results then.
margaret gillon
Ranch Hand

Joined: Nov 12, 2008
Posts: 318
    
    5

Tuesday night my Windows 7, 64 bit JRE stopped working and ANT would not run. Then the registry crashed. I removed the JDK, the JRE, and ANT. I cleaned the registry by hand, made sure the cleanup was reflected in the control panel, and made some system backups. The version I removed was JDK 1.6.0_38.

Tonight I have installed the final JDK6, JDK 1.6.0_45.

Windows 7, 64 bit, JDK 1.6.0_45, output from the small build.xml Greg posted on Tuesday, July 16, 2013 2:06:01 PM

margaret gillon
Ranch Hand

Joined: Nov 12, 2008
Posts: 318
    
    5

On my Windows 7, 64 bit, machine, JDK 1.6.0_45

Here is a modified build.xml from Greg on 7/17/2013, 9:09 AM. I am running on the ReverseServlet that this thread was started with



Here is the output from the build.xml



c:\java\ReverseServlet>ant -verbose
Apache Ant(TM) version 1.8.4 compiled on May 22 2012
Trying the default build file: build.xml
Buildfile: c:\java\ReverseServlet\build.xml
Detected Java version: 1.6 in: C:\Program Files\Java\jre6
Detected OS: Windows 7
parsing buildfile c:\java\ReverseServlet\build.xml with URI = file:/c:/java/Reve
rseServlet/build.xml
Project base dir set to: c:\java\ReverseServlet
Build sequence for target(s) `init' is [init]
Complete build sequence is [init, ]

init:
parsing buildfile jar:file:/C:/ant/lib/ant.jar!/org/apache/tools/ant/antlib.xml
with URI = jar:file:/C:/ant/lib/ant.jar!/org/apache/tools/ant/antlib.xml from a
zip file
[property] Loading Environment env.
[available] Found: c:\tomcat\lib\servlet-api.jar
[echo] c:\tomcat
[echo] c:\tomcat
[echo] c:\tomcat/lib/servlet-api.jar
[echo] Jar found: true

BUILD SUCCESSFUL
Total time: 0 seconds

c:\java\ReverseServlet>
margaret gillon
Ranch Hand

Joined: Nov 12, 2008
Posts: 318
    
    5

On my Windows 7, 64 bit, machine, JDK 1.6.0_45
Now I am running the first build.xml that Greg posted on in this thread Sunday, July 07, 2013 3:31:40 PM . The build.xml works on Greg's Windows 7 machine. On my Windows 7 machine using the 7/7/13 build.xml the errors still exist that cannot find the servlet classes. This is the verbose output from the same project that I just ran ANT on in the previous post...



c:\java\ReverseServlet>ANT -verbose
Apache Ant(TM) version 1.8.4 compiled on May 22 2012
Trying the default build file: build.xml
Buildfile: c:\java\ReverseServlet\build.xml
Detected Java version: 1.6 in: C:\Program Files\Java\jre6
Detected OS: Windows 7
parsing buildfile c:\java\ReverseServlet\build.xml with URI = file:/c:/java/Reve
rseServlet/build.xml
Project base dir set to: c:\java\ReverseServlet
Build sequence for target(s) `build' is [init, initdirs, compile, war, build]
Complete build sequence is [init, initdirs, compile, war, build, deploy, clean,
ffbuild, ]

init:
parsing buildfile jar:file:/C:/ant/lib/ant.jar!/org/apache/tools/ant/antlib.xml
with URI = jar:file:/C:/ant/lib/ant.jar!/org/apache/tools/ant/antlib.xml from a
zip file
[property] Loading Environment env.

initdirs:
[mkdir] Skipping c:\java\ReverseServlet\build\classes because it already exi
sts.
[mkdir] Skipping c:\java\ReverseServlet\build\dist because it already exists
.

compile:
[echo] C:\Users\mmg
[javac] c:\java\ReverseServlet\build.xml:22: warning: 'includeantruntime' wa
s not set, defaulting to build.sysclasspath=last; set to false for repeatable bu
ilds
[javac] com\javaranch\drive\ReverseServlet.java added as com\javaranch\drive
\ReverseServlet.class doesn't exist.
[javac] Compiling 1 source file to c:\java\ReverseServlet\build\classes
[javac] Using modern compiler
dropping c:\java\ReverseServlet\{servlet.jar} from path as it doesn't exist
[javac] Compilation arguments:
[javac] '-d'
[javac] 'c:\java\ReverseServlet\build\classes'
[javac] '-classpath'
[javac] 'c:\java\ReverseServlet\build\classes;c:\ant\lib\ant-launcher.jar;C:
\Program Files\Java\jdk1.6.0_45\bin;C:\ant\bin;C:\tomcat\lib;C:\ant\lib\ant-antl
r.jar;C:\ant\lib\ant-apache-bcel.jar;C:\ant\lib\ant-apache-bsf.jar;C:\ant\lib\an
t-apache-log4j.jar;C:\ant\lib\ant-apache-oro.jar;C:\ant\lib\ant-apache-regexp.ja
r;C:\ant\lib\ant-apache-resolver.jar;C:\ant\lib\ant-apache-xalan2.jar;C:\ant\lib
\ant-commons-logging.jar;C:\ant\lib\ant-commons-net.jar;C:\ant\lib\ant-jai.jar;C
:\ant\lib\ant-javamail.jar;C:\ant\lib\ant-jdepend.jar;C:\ant\lib\ant-jmf.jar;C:\
ant\lib\ant-jsch.jar;C:\ant\lib\ant-junit.jar;C:\ant\lib\ant-junit4.jar;C:\ant\l
ib\ant-netrexx.jar;C:\ant\lib\ant-swing.jar;C:\ant\lib\ant-testutil.jar;C:\ant\l
ib\ant.jar;C:\Program Files\Java\jre6\lib\tools.jar'
[javac] '-sourcepath'
[javac] 'c:\java\ReverseServlet\src\java'
[javac] '-g:none'
[javac]
[javac] The ' characters around the executable and arguments are
[javac] not part of the command.
[javac] File to be compiled:
[javac] c:\java\ReverseServlet\src\java\com\javaranch\drive\ReverseServl
et.java
[javac] c:\java\ReverseServlet\src\java\com\javaranch\drive\ReverseServlet.j
ava:4: package javax.servlet.annotation does not exist
[javac] import javax.servlet.annotation.WebServlet;
[javac] ^
[javac] c:\java\ReverseServlet\src\java\com\javaranch\drive\ReverseServlet.j
ava:5: package javax.servlet.http does not exist
[javac] import javax.servlet.http.HttpServlet;
[javac] ^
[javac] c:\java\ReverseServlet\src\java\com\javaranch\drive\ReverseServlet.j
ava:6: package javax.servlet.http does not exist
[javac] import javax.servlet.http.HttpServletRequest;
[javac] ^
[javac] c:\java\ReverseServlet\src\java\com\javaranch\drive\ReverseServlet.j
ava:7: package javax.servlet.http does not exist
[javac] import javax.servlet.http.HttpServletResponse;
[javac] ^
[javac] c:\java\ReverseServlet\src\java\com\javaranch\drive\ReverseServlet.j
ava:10: cannot find symbol
[javac] symbol: class HttpServlet
[javac] public class ReverseServlet extends HttpServlet
[javac] ^
[javac] c:\java\ReverseServlet\src\java\com\javaranch\drive\ReverseServlet.j
ava:9: cannot find symbol
[javac] symbol: class WebServlet
[javac] @WebServlet("/ReverseServlet")
[javac] ^
[javac] c:\java\ReverseServlet\src\java\com\javaranch\drive\ReverseServlet.j
ava:15: cannot find symbol
[javac] symbol : class HttpServletRequest
[javac] location: class com.javaranch.drive.ReverseServlet
[javac] public void doPost(HttpServletRequest request, HttpServletRespon
se response)
[javac] ^
[javac] c:\java\ReverseServlet\src\java\com\javaranch\drive\ReverseServlet.j
ava:15: cannot find symbol
[javac] symbol : class HttpServletResponse
[javac] location: class com.javaranch.drive.ReverseServlet
[javac] public void doPost(HttpServletRequest request, HttpServletRespon
se response)
[javac] ^
[javac] c:\java\ReverseServlet\src\java\com\javaranch\drive\ReverseServlet.j
ava:14: method does not override or implement a method from a supertype
[javac] @Override
[javac] ^
[javac] 9 errors

BUILD FAILED
c:\java\ReverseServlet\build.xml:22: Compile failed; see the compiler error outp
ut for details.
at org.apache.tools.ant.taskdefs.Javac.compile(Javac.java:1174)
at org.apache.tools.ant.taskdefs.Javac.execute(Javac.java:930)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.jav
a:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.Target.execute(Target.java:392)
at org.apache.tools.ant.Target.performTasks(Target.java:413)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1399)
at org.apache.tools.ant.Project.executeTarget(Project.java:1368)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExe
cutor.java:41)
at org.apache.tools.ant.Project.executeTargets(Project.java:1251)
at org.apache.tools.ant.Main.runBuild(Main.java:811)
at org.apache.tools.ant.Main.startAnt(Main.java:217)
at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280)
at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109)

Total time: 1 second

c:\java\ReverseServlet>


Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2848
    
  11

Cool, that's what I needed! It looks like it was my fault after all.

Change:

on line 24, to:



The weird thing is I'm looking at the file that I thought I pasted that from, and it's not missing the $. So, what happened? Did JavaRanch get hacked by extremely selective vandals? I have no idea. Apologies for all the agony though! Hopefully it will work all right now.
margaret gillon
Ranch Hand

Joined: Nov 12, 2008
Posts: 318
    
    5

The correction to line 24 fixes the build problem on my Windows 7 64 bit machine.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ANT does not use %CATALINA_HOME% to find servlet-api.jar on Windows 7 64 bit and build fails