This approach is not based on the Tomcat Ant Tasks or a brute force <exec dir=”${tomcat.home}/bin” executable=”startup.bat”/> kind of approach.
After wading through startup.bat and catalina.bat I figured that they boil down to the following:
<target name="tomcat-start">
<java jar="${tomcat.home}/bin/bootstrap.jar" fork="true">
<jvmarg value="-Dcatalina.home=${tomcat.home}"/>
</java>
</target>
<target name="tomcat-stop">
<java jar="${tomcat.home}/bin/bootstrap.jar" fork="true">
<jvmarg value="-Dcatalina.home=${tomcat.home}"/>
<arg line="stop"/>
</java>
</target>
You can have in build.properties, something like the following entry:
tomcat.home=E:/peter/opt/apache-tomcat-5.5.12
It works great and in Eclipse and NetBeans the stack traces within the log window hyperlink to the source code just as you would expect. You would normally combine these ant targets with a “war” task. The Tomcat Reload Ant task can also be used to speed up development time.
One of the advantages of this approach is that you can extend it to start Tomcat for remote debugging
<target name="tomcat-start-debug">
<java jar="${tomcat.home}/bin/bootstrap.jar" fork="true">
<jvmarg value="-Dcatalina.home=${tomcat.home}"/>
<jvmarg value="-Xdebug"/>
<jvmarg
value="-Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n"/>
</java>
</target>





Congrats!.
Is one of the best approaches to manage tomcat ignition.
Thanks a lot.
Comment by Alex. S. — April 3, 2006 @ 10:13 pm
Works great!
One minor adjustment if you have spaces in your tomcat.home. For example: C:\Program Files\Apache Software Foundation\Tomcat 5.5
Tokenize the second ${tomcat.home} value:
Thanks!
Comment by Mark G — July 27, 2006 @ 2:29 am
Hello
When I try to implement the tomcat-start and stop i got the error like
tomcat-stop:
[java] java.lang.NoClassDefFoundError: Files/Apache
[java] Exception in thread “main”
[java] Java Result: 1
Please help me why this error show up.
Comment by sbaidya — August 15, 2006 @ 1:47 pm
Thanks Mark G & sbaidya for helping me identify a subtle bug in the snippets. When the path of “tomcat.home” has spaces, then “jvmarg line” usage would make the JVM think that the line contained multiple ‘tokens’
The solution is to use “jvmarg value” and ant will do the rest. I have updated the post.
Comment by Peter Thomas — August 15, 2006 @ 9:42 pm
It works fine dude! Good job! Thanks.
Comment by iktuz — August 18, 2006 @ 1:51 am
How do you configure this so that Tomcat is launched in a separate command line?
Comment by Ken Hammond — September 4, 2006 @ 4:50 am
You mean something like a DOS window? I don’t think that’s possible with Ant. Not sure why you’d want this and what I would do is use the normal *.bat / *.sh scripts.
Note that when launching within Eclipse or NetBeans, any error stack traces hyperlink to source code, which is a huge time saver.
Comment by Peter Thomas — September 4, 2006 @ 9:49 am
[...] Grâce à un collègue, j’ai enfin trouvé une façon simple de démarrer et d’arrêter Tomcat depuis mon build Ant. La seule chose qui lui manque, évidement, c’est la possibilité d’activer ou de désactiver ces tâches simplement. Pour moi c’est facile, grâce à ma technique des flags autogénérés : PLAIN TEXT XML: [...]
Pingback by Le blog » Archive du blog » Lancer et arrêter Tomcat dans Ant — October 10, 2006 @ 1:05 pm
Arrêter et démarrer Tomcat en mode debug
What I wrote in previous message was a little too much optimistic …
In fact, it is not possible stoppong topcat using topcat-stop from previous message. The reason is quite simple : since I start Tomcat in debug mode, that’s to say with -XDebug opti…
Trackback by Le blog — October 10, 2006 @ 2:11 pm
very good example ….if possible please put some more examples on different ant tasks that can be easily done in similar fashion.
Comment by samir — October 10, 2006 @ 2:38 pm
[...] This example is a bit more complex than the one in this previous post How to start and stop Tomcat from Ant. Here, reuse of Ant code using “macrodef” is attempted. Also the start target is smart enough to stop the server if it is already running and the “waitfor” (with the hard-coded true == false) is to give enough time for the server (2 seconds) to shutdown before restarting. I am using this approach successfully for Jetty 6.X [...]
Pingback by How to start and stop Jetty from Ant « Incremental Operations — October 10, 2006 @ 8:10 pm
hi, can u plz tell me how can I stop and restart tomcat remotly.
Thanks.
the code I am using is:
===================================1. target name=”tomcat-stop22222″
2. telnet userid=”retadmin”
password=”wed@day”
server=”iserver” port=”40″
timeout=”20″
3. write echo=”false”
stop tomcat5
write
4. telnet
5. target
Comment by dev — December 20, 2006 @ 5:46 pm
hi,this code worked perfectly… thanks a lot….
but when i start executing it using
it didnt worked.It is giving the error saying
Execute failed:
java.io.IOException: CreateProcess: startup.bat error=2
But when i execute this command to open a simple notepad application, it perfectly worked
Could you tell me the solution for this?
Comment by vinayak — December 29, 2006 @ 2:19 pm
Hi,
It is working! Thanks for the script.
I used the content of the tasks and defined them in Eclipse like External Tools. Here is the configuration for Start Tomcat:
In Main Tab:
Location: ${java_home}/bin/java.exe
Working Directory: ${project_loc}
Arguments: -Dcatalina.home=${tomcat_home} -Dorg.apache.tapestry.disable-caching=true -jar ${tomcat_home}/bin/bootstrap.jar
In Common Tab check “Laucnh in backgrund”
in Console View you get the output.
For Debug and Stop is similar.
Comment by Claudiu Dobre — March 1, 2007 @ 3:02 pm
If you want to start and stop tomcat conditionally the following works for me.
Assumptions:
A. Tomcat uses the port 8080 (this is the default)
B. You have downloaded the latest ant-contrib.jar from http://ant-contrib.sourceforge.net/
C. The latest ant-contrib.jar is named ant-contrib-1.0b3.jar (the latest version will probably be called something different so rename in the code below as appropriate)
1. Place ant-contrib-1.0b3.jar into the base directory of your project.
3. Add the following code to the build.xml
<taskdef resource=”net/sf/antcontrib/antcontrib.properties”>
<classpath>
<pathelement location=”${basedir}/ant-contrib-1.0b3.jar”/>
</classpath>
</taskdef>
4. Change your tomcat-start task to:
<target name=”tomcat-start”>
<if><not><http url=”http://localhost:8080″/></not><then>
<java jar=”${tomcat.home}/bin/bootstrap.jar” fork=”true”>
<jvmarg value=”-Dcatalina.home=${tomcat.home}”/>
</java>
</then></if>
</target>
5. Apply the same logic to the tomcat-stop task (the same change but without the <not></not>)
Comment by s_t — March 12, 2007 @ 11:55 pm
PS – there is no step 2 :)
Comment by s_t — March 13, 2007 @ 12:07 am
Download ant-contrib: isn’t that step 0? :P
Honestly I don’t like extra JAR files having to be placed in the project folder structure.
Why do you need the extra JAR file if you can do without it?
You should read a more recent blog post of mine that shows how to conditionally stop and start Jetty using Ant. The same principles will apply to Tomcat of course.
http://ptrthomas.wordpress.com/2006/10/10/how-to-start-and-stop-jetty-from-ant/
Comment by Peter Thomas — March 14, 2007 @ 2:21 pm
Is there anyway to start tomcat thru tomcat plugin for eclipse, using ANT.
for instance, i want to run my ant build scripts and automatically start tomcat within eclipse, once build is complete,.
Comment by Sunny — March 20, 2007 @ 8:06 am
Suggestion, don’t use the tomcat plugin for eclipse :)
Also look at the link in my previous comment.
Comment by Peter Thomas — March 20, 2007 @ 4:21 pm
Works, but when I invoke tomcat-start from my build.xml in the command line, the build.xml never ends and the server console appears to be dumping out to the window I invoked my build.xml from. The last thing I see (if I do not access my app) is Server startup time in the window. Any ideas?
Comment by Warren — March 28, 2007 @ 4:31 am
This is normal and you get to see the log in the console just as you would if you used Tomcat’s start.bat. You do have to open a new console and run “ant tomcat-stop”, CTRL-C would also work of course.
Thing is – within Eclipse and NetBeans, with the built-in Ant support – it is very easy to select and run an Ant target – e.g. double-click within the Ant view for Eclipse and I can create toolbar shortcuts to Ant targets in NetBeans.
Comment by Peter Thomas — March 28, 2007 @ 1:42 pm
thx for this easy solution, but i have still a problem. I want ant to delete the old webapp before deploying a new one. So it´s necessary the tomcat is stopped. How check the status and then go on with the deleting task? thx a lot
Comment by ray — July 6, 2007 @ 1:52 am
Yes, have a look at the link at comment #17 and that should be exactly what you need. Although that example uses Jetty, it should be very easy for you to apply it to Tomcat.
Checking if the web server is running or not is as easy as using the ant “socket” task.
Comment by Peter Thomas — July 6, 2007 @ 10:58 am
The solution to this is to add spawn=”true” to the ANT task.
Warren said on March 28, 2007:
“Works, but when I invoke tomcat-start from my build.xml in the command line, the build.xml never ends and the server console appears to be dumping out to the window I invoked my build.xml from. The last thing I see (if I do not access my app) is Server startup time in the window. Any ideas?”
Comment by Stewart — August 7, 2007 @ 8:56 pm
Thank you Peter(17) and Stewart(24) – that is just what I needed :)
Comment by Andrew — August 8, 2007 @ 7:53 pm
Thanks Peter! This approach works great! I have been trying to get Tomcat running using the Tomcat-Ant-Tasks http://tomcat.apache.org/tomcat-5.5-doc/manager-howto.html#Executing%20Manager%20Commands%20With%20Ant . But I keep on getting an Exception when I try to start Tomcat:
java.net.ConnectException: Connection refused: connect.
I have tried deploy, reload and undeploy and they work fine. any ideas why. Thanks again.
Comment by Abhishek — August 22, 2007 @ 5:45 pm
Really hard to tell Abhishek, but looks like something in your Ant target is trying to connect to Tomcat before it is started and obviously cannot. Maybe you have something mixed up?
I want to comment on #24 – I am not sure if spawn=”true” is a good idea because then the Tomcat console will not be visible. You will never know if Tomcat is running or not and it will be much more difficult to “kill” the process.
Comment by Peter Thomas — August 23, 2007 @ 10:35 am
Hi,
I’ve tried this method which seems to work fine, except that due to my configuration, I need to provide Tomcat with a set of “config” files before it starts. I’ve tried to set the nested “classpath” tag in the “java” tag but it doesn’t work: files are still unfounded by the classLoader.
If anyone can help on this….
Comment by John J Rambo — September 5, 2007 @ 7:48 pm
John: for the “java” task, instead of using the “jar” attribute, try using the “classname” attribute with a value of “org.apache.catalina.startup.Bootstrap”
And now the nested “classpath” tag should take effect. You should add “${tomcat.home}/bin/bootstrap.jar” to the classpath of course.
Do let me know if this works.
Comment by Peter Thomas — September 7, 2007 @ 11:58 pm
HI Peter
how to refresh the tomcat cache with out restarting it when there is any change in properties file (for e.g loacl.properties).
i guess reloadable=true in conf/server.xml will be for .java files
and in case of jsp files if there is any timestamp difference in .jsp and .class contents of tomcat/work folder will get refreshed
but i have no clue how to handle .properties or xml files
Comment by Vikram — September 26, 2007 @ 4:36 pm
Not sure Vikram.
Maybe if you know which Java file is responsible for re-loading the *.properties or *.xml file – you can use the Ant “touch” task:
http://ant.apache.org/manual/CoreTasks/touch.html
Comment by Peter Thomas — September 26, 2007 @ 9:50 pm
Thanks Peter i will try this and let u know
Comment by Vikram — September 27, 2007 @ 10:28 am
Hi Peter
i got the solution , by enabling Reload an existing application using Tomcat Manager ,This can be useful when the web application context is not reloadable and you have updated classes or property files in the /WEB-INF/classes directory or when you have added or updated jar files in the /WEB-INF/lib directory. I have implemented this in a sample web app by changing values in properties file with out restarting it . it worked fine. Thorough testing needs to be done
i referred this link
http://tomcat.apache.org/tomcat-5.0-doc/manager-howto.html#Reload%20An%20Existing%20Application
Vikram
Comment by Vikram — October 1, 2007 @ 10:01 am
Hi All,
If you have setup Tomcat as a service and want to initiate this service via Ant you can do the following. Execute tomcat as a service when the service is called Tomcat5
<target name="tomcat-start-debug">
<exec executable="net">
<arg value="start"/>
<arg value="Tomcat5"/>
</exec>
</target>
<target name="tomcat-stop">
<exec executable="net">
<arg value="stop"/>
<arg value="Tomcat5"/>
</exec>
</target>
Keeps the ant tasks clean and all the arguments within the service that can be managed via the Tomcat monitor.I have tested this with Tomcat 5.0.
Renny
Comment by Renny Nanaiah — November 7, 2007 @ 8:48 am
Thanks Renny, of course I guess this works only on Windows :) And developers are assumed to have the Tomcat service installed.
The Ant based approach in the blog post works anywhere and is simpler to roll out to large teams. You just have to extract the zip distribution of Tomcat.
Comment by Peter Thomas — November 7, 2007 @ 11:14 pm
Excellent. Thanks you saved me alot of time. Very useful.
Comment by Jim Oberan — December 7, 2007 @ 11:26 pm
I am using the approach described in the post on Win XP, but have noticed that though Tomcat is stopped, the java process that was forked remains alive.
This is bad for since it keeps locks on files that I’d like to change.
Has anyone experienced this? Have a solution to have the java executable terminate ?
Comment by Ofer — January 5, 2008 @ 6:53 am
I’ve seen this happen when a web app deployed within Tomcat has a problem shutting down gracefully, specifically when I had Quartz mis-configured once.
Try to see if a ‘vanilla’ Tomcat shuts down fine with this approach and then you can confirm if it indeed is a problem with your WAR.
Comment by Peter Thomas — January 5, 2008 @ 12:06 pm
That was it. Thanks !
Comment by Ofer — January 6, 2008 @ 7:38 am
WHEN I USE THE DOWN ANT SCRIPT I GET java.net.ConnectException: Connection refused: connect
********************************
********************************
ALL THE Exception IS:
tomcat-stop:
[java] 21/01/2008 12:02:39 ص org.apache.catalina.startup.Catalina stopServer
[java] SEVERE: Catalina.stop:
[java] java.net.ConnectException: Connection refused: connect
[java] at java.net.PlainSocketImpl.socketConnect(Native Method)
[java] at java.net.PlainSocketImpl.doConnect(Unknown Source)
[java] at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
[java] at java.net.PlainSocketImpl.connect(Unknown Source)
[java] at java.net.SocksSocketImpl.connect(Unknown Source)
[java] at java.net.Socket.connect(Unknown Source)
[java] at java.net.Socket.connect(Unknown Source)
[java] at java.net.Socket.(Unknown Source)
[java] at java.net.Socket.(Unknown Source)
[java] at org.apache.catalina.startup.Catalina.stopServer(Catalina.java:395)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
[java] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
[java] at java.lang.reflect.Method.invoke(Unknown Source)
[java] at org.apache.catalina.startup.Bootstrap.stopServer(Bootstrap.java:344)
[java] at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:435)
[java] Java Result: 1
BUILD SUCCESSFUL
Comment by PAKRY — January 21, 2008 @ 1:40 pm
ALSO THE TOMCAT SERVER STIL WORK AND NOT STOP WHERE THE PROBLEM IN THE SCRIPT OF (TOMCAT-STOP)
Comment by PAKRY — January 21, 2008 @ 1:50 pm
PAKRY: looks like the Server port (default 8005) on which Tomcat listens for shutdown requests is blocked, you can try to change it by editing conf/server.xml
Comment by Peter Thomas — January 21, 2008 @ 2:38 pm
Dear, the start target is indeed working BUT as soon as I close the dow window in which the ANT was running, the tomcat is stopped. I think it is because the target is not run in the background (like one would do in UNIX with the & switch) … How can we achieve this using the target as in your excellent article?
thx a lot
Comment by SWI — February 5, 2008 @ 7:20 pm
Dear,
Sorry for the previous post, I did not see the question was already answered, the spawn did indeed do the trick.
thx a lot
PS This is the code then:
Comment by SWI — February 5, 2008 @ 7:31 pm
how to start or stop web application instance in tomcat(not full tomcat) using ant script?
Comment by Neeraj — April 9, 2008 @ 5:12 pm
@Neeraj – refer the link to Tomcat Ant Tasks that appears at the start of the blog post.
Comment by Peter Thomas — April 9, 2008 @ 7:14 pm
Great post but I have one problem I have spent many hours trying to resolve. Any suggestions will be appreciated.
Here’s the background: On my local machine I do development work for a web appl in Java 1.4 and Tomcat 5.02 to be compatible with the shared hosting environment where it will be deployed. I also have Java 6 installed on my local machine.
Here’s the problem: The server starts and runs perfectly until a new JSP gets deployed and the server tries to compile it. Then I get an error telling me that rt.jar has the wrong version of Object and it is plain from the error message that it is looking at the Java 6 version of rt.jar.
Obviously the compile step is not using the same classpath as the start task. I tried the technique in comment 29 but nothing changed. I have the java home and classpath system variables pointing at Java 1.4. I’m no registry expert but I suppose there must be something in there that makes Java 6 the default but actually I want it to be except for this application. There’s bound to be a way to explicitly set the classpath for the JSP compiles in Tomcat but I haven’t been able to find it and it’s apparently not the classpath used in the Ant start task.
Here’s my Ant task, but as I said it appears the classpath here has nothing to do with the classpath used to compile JSP’s.
Comment by skeezicks — July 17, 2008 @ 7:26 am
Sorry but the Ant task was stripped off my comment…trying again…
Comment by skeezicks — July 17, 2008 @ 7:30 am
Hi
May i know what is the command for prompting either yes or no option for performing some action based on prompt value like Deletion or Compiling or Building files.Kindly provide the solution ASAP.
Comment by Neeta — July 31, 2008 @ 4:14 pm
@Neeta – here you go:
<input message="Are you sure?" validargs="y,n" addproperty="input"/> <condition property="abort"> <equals arg1="n" arg2="${input}"/> </condition> <fail if="abort">User aborted.</fail>Comment by Peter Thomas — July 31, 2008 @ 6:11 pm
Hi Peter
Thanks a lot.Its working fine.I have one more Question like i have two locations,in which i have to copy the data from the selected location by the user.Is it possible to write to avoid redundancy.
The snippet of code is as follows where am facing problem
I have to specify dynamically assign the value which user selected option to the pathvariable(Either RelArea or SVNArea).Is there any possiblities to meet this requirements.
Comment by Neeta — August 1, 2008 @ 11:40 am
[...] just need the light-weight Java SE version of NetBeans in order to start and stop Tomcat, debug and even hot-deploy classes for your web-app. No extra plugins are required for e.g. Maven [...]
Pingback by An alternative Maven plugin for Ant and NetBeans « Incremental Operations — August 17, 2008 @ 8:34 pm
i can’t start my tomcat server i have tomcat 6.x and jdk 1.6 installed and im using myeclipse 5.5.. when i start the tomcat server it waits for aplication server ” trying to connect to application server” and it waits only…. my application server is RMI class and it is working… i mean it’s already started but my tomcat can’t detect it… what is the problem ??????????
Comment by raj — November 12, 2008 @ 5:05 pm
Here is the solution that recreated tomcat service each time. That is very useful if you have to maintain server hosts running the same application and doing changes in tomcat service setup.
http://gusiev.com#Ant%20task%20to%20install%20tomcat%20service
Comment by Bogdan — April 30, 2009 @ 1:06 am
[...] myself, except the “tomcat-start” and “tomcat-stop” targets, original code here. This code is subjet to improvements, modifications and of course, can have a lot of bugs… [...]
Pingback by How to run a seam-gen project in Tomcat 6 | InteliDev — April 30, 2009 @ 3:18 am
Thanks man, nice post
Comment by Balaji — May 15, 2009 @ 6:12 pm
If I want to launch my webbrowser with a url right after starting up Tomcat, how can I determine if Tomcat has finished loading?
In my ANT script I start up Tomcat, then I call
If I use spawn=true for the tomcat startup, then I will not see the tomcat output, and my browser launches and loads the url.
However, if I want to see the tomcat output and remove the spawn attribute, my web browser never launches – the console window just shows the tomcat output.
Why is this?
Comment by Flex coder — August 15, 2009 @ 11:56 am
@Flex coder: look at the Ant snippet at the end of this post, specifically the “waitfor” element:
http://ptrthomas.wordpress.com/2009/01/24/how-to-start-and-stop-jetty-revisited/
Comment by Peter Thomas — August 15, 2009 @ 11:58 am
Hmm…my code didn’t post…here’s another try
Comment by Flex coder — August 15, 2009 @ 12:02 pm
Thanks Peter – that helped a lot!
Comment by Flex coder — August 15, 2009 @ 12:36 pm
I’ve implemented the waitfor and my script runs – the browser launches successfully etc. The only problem is the console window displays the tomcat startup log – the rest of my ant script – the part that says “Build completed – time 1 min 2 sec” is not being output to the console.
In eclipse, I have to click on my ant file again and run it, this causes the last bit of output to be printed to the console.
I’ve read the posts above, but do not want to use spawn=”true” – I want to see the tomcat startup along with the rest of my ant output.
Is there a workaround for this?
Comment by Flex coder — August 15, 2009 @ 9:52 pm
@Flex coder: hmm, not sure.
But do you really need this? The way I normally work is I start tomcat from ant within eclipse and it occupies a console. When I double-click on another ant target to stop tomcat, the original console will gracefully end with the build complete message etc.
Comment by Peter Thomas — August 15, 2009 @ 11:06 pm
The problem is with the environnment that has been set up for me to work with.
I have a Flex front end project that talks to a database backend – the pre-existing ANT script setup by our lead developer does a whole bunch of fancy stuff – cleans the output directories, builds the war and copies it over to our tomcat server etc. Tomcat needs to be stopped for the script to run and the build to complete.
When coding the UI in flex it gets very repetitious. Here’s the way I have been working for the past year. After any code change I have to 1) stop tomcat, 2) build the ANT script – wait till its done, 3) restart tomcat 4) launch my browser with the application url or run it from eclipse with the run dialog and then 5) clean the browser cache 6) click login from our Flex UI to login to the app since it uses Spring Security.
This gets very very tedious after doing it hundreds of times a day.
So I thought that it would be great (just like when I used to work with .NET and Visual Studio) if I could just hit 1 button to run everything.
So far I’ve got it all working for the most part now – my ant script does all of the above – it’s just that the console output hangs at Tomcat startup and I don’t see the ‘Build complete’ message.
I guess I could remove the Tomcat startup code and the exec which loads my browser and put those in a separate ant script that I could click on and run after I see the ‘Build successful’ message. At least 2 clicks are better than the way I was doing it before!
Thanks for the previous link – it helped me to get the current setup working. I could just use spawn=”true” to suppress the Tomcat output and it will all work – it’s just that I would like to have a log for Tomcat.
Comment by Flex coder — August 16, 2009 @ 2:21 am
exec dir=”${env.TOMCAT_HOME}/bin” executable=”startup.bat”
spawn=”true” vmlauncher=”true” /
Comment by Sachin Malkar — November 20, 2009 @ 5:14 pm
@Sachin – please read the FIRST sentence of the blog post VERY CAREFULLY.
Comment by Peter Thomas — November 20, 2009 @ 11:35 pm