How to start and stop Tomcat from ANT

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>
About these ads

104 Responses to How to start and stop Tomcat from ANT

  1. Alex. S. says:

    Congrats!.

    Is one of the best approaches to manage tomcat ignition.

    Thanks a lot.

  2. Mark G says:

    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!

  3. sbaidya says:

    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.

  4. Peter Thomas says:

    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.

  5. iktuz says:

    It works fine dude! Good job! Thanks.

  6. Ken Hammond says:

    How do you configure this so that Tomcat is launched in a separate command line?

  7. Peter Thomas says:

    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.

  8. Pingback: Le blog » Archive du blog » Lancer et arrêter Tomcat dans Ant

  9. Pingback: Le blog

  10. samir says:

    very good example ….if possible please put some more examples on different ant tasks that can be easily done in similar fashion.

  11. Pingback: How to start and stop Jetty from Ant « Incremental Operations

  12. dev says:

    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

  13. vinayak says:

    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?

  14. Claudiu Dobre says:

    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.

  15. s_t says:

    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>)

  16. s_t says:

    PS – there is no step 2 :)

  17. Peter Thomas says:

    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/

  18. Sunny says:

    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,.

  19. Peter Thomas says:

    Suggestion, don’t use the tomcat plugin for eclipse :)

    Also look at the link in my previous comment.

  20. Warren says:

    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?

  21. Peter Thomas says:

    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.

  22. ray says:

    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

  23. Peter Thomas says:

    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.

  24. Stewart says:

    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?”

  25. Andrew says:

    Thank you Peter(17) and Stewart(24) – that is just what I needed :)

  26. Abhishek says:

    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.

  27. Peter Thomas says:

    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.

  28. John J Rambo says:

    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….

  29. Peter Thomas says:

    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.

  30. Vikram says:

    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

  31. Peter Thomas says:

    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

  32. Vikram says:

    Thanks Peter i will try this and let u know

  33. Vikram says:

    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

  34. Renny Nanaiah says:

    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

  35. Peter Thomas says:

    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.

  36. Jim Oberan says:

    Excellent. Thanks you saved me alot of time. Very useful.

  37. Ofer says:

    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 ?

  38. Peter Thomas says:

    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.

  39. Ofer says:

    That was it. Thanks !

  40. PAKRY says:

    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

  41. PAKRY says:

    ALSO THE TOMCAT SERVER STIL WORK AND NOT STOP WHERE THE PROBLEM IN THE SCRIPT OF (TOMCAT-STOP)

  42. Peter Thomas says:

    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

  43. SWI says:

    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

  44. SWI says:

    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:

  45. Neeraj says:

    how to start or stop web application instance in tomcat(not full tomcat) using ant script?

  46. Peter Thomas says:

    @Neeraj – refer the link to Tomcat Ant Tasks that appears at the start of the blog post.

  47. skeezicks says:

    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.

  48. skeezicks says:

    Sorry but the Ant task was stripped off my comment…trying again…

  49. Neeta says:

    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.

  50. Peter Thomas says:

    @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>
    
  51. Neeta says:

    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.

  52. Pingback: An alternative Maven plugin for Ant and NetBeans « Incremental Operations

  53. raj says:

    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 ??????????

  54. Bogdan says:

    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

  55. Pingback: How to run a seam-gen project in Tomcat 6 | InteliDev

  56. Balaji says:

    Thanks man, nice post

  57. Flex coder says:

    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?

  58. Peter Thomas says:

    @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/

  59. Flex coder says:

    Hmm…my code didn’t post…here’s another try

  60. Flex coder says:

    Thanks Peter – that helped a lot!

  61. Flex coder says:

    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?

  62. Peter Thomas says:

    @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.

  63. Flex coder says:

    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.

  64. Sachin Malkar says:

    exec dir=”${env.TOMCAT_HOME}/bin” executable=”startup.bat”
    spawn=”true” vmlauncher=”true” /

  65. Peter Thomas says:

    @Sachin – please read the FIRST sentence of the blog post VERY CAREFULLY.

  66. Rahi says:

    How do I gracefully stop webapp within tomcat (5.5.2x)using windows batch file ? I don’t want to stop tomcat itself !!!

  67. Peter Thomas says:

    @Rahi You can’t. Use Ant. follow the links at the start of this blog post. If you are really want to use batch files (yuck), write a batch file that invokes Ant.

  68. Rahi says:

    I am not fond of batch files. Any script that works on windows is fine.
    Thanks for quick reply..I will check the links.

  69. sam arul raj.T says:

    E:\SAM-PC\springapp>ant install
    Buildfile: build.xml

    install:

    BUILD FAILED
    java.io.IOException: Server returned HTTP response code: 401 for URL: http://loc
    alhost:8080/manager/install?path=%2Fspringapp&war=springapp
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLCon
    nection.java:1245)
    at org.apache.catalina.ant.AbstractCatalinaTask.execute(AbstractCatalina
    Task.java:223)
    at org.apache.catalina.ant.AbstractCatalinaTask.execute(AbstractCatalina
    Task.java:146)
    at org.apache.catalina.ant.InstallTask.execute(InstallTask.java:114)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:288)
    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:357)
    at org.apache.tools.ant.Target.performTasks(Target.java:385)
    at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1337)
    at org.apache.tools.ant.Project.executeTarget(Project.java:1306)
    at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExe
    cutor.java:41)
    at org.apache.tools.ant.Project.executeTargets(Project.java:1189)
    at org.apache.tools.ant.Main.runBuild(Main.java:758)
    at org.apache.tools.ant.Main.startAnt(Main.java:217)
    at org.apache.tools.ant.launch.Launcher.run(Launcher.java:257)
    at org.apache.tools.ant.launch.Launcher.main(Launcher.java:104)

    Total time: 0 seconds

    i am getting the above error ,may i know wht mistake i made in my application

  70. davy says:

    it exactly what i am looking for.
    Thank you

  71. Rohitsingh says:

    hi when i start tomcat using ant target i get this kind of error
    C:\Users\rohitsi\Desktop\DynamicCYBMonitor\build.xml:96: java.net.ConnectException: Connection refused: connect please help

  72. B.Doyle says:

    One step further and continue after tomcat starts using the parallel task with daemons

    <parallel>
    	<daemons>
    		<java jar="${tomcat.home}/bin/bootstrap.jar" fork="true">
    			<jvmarg value="-Dcatalina.home=${tomcat.home}"/>
    		</java>
    	</daemons>
    	<sequential>
    		<sleep seconds="10"/>
    		<echo message="list" />
    		<list url="${tomcat.manager.url}" 
    			username="${tomcat.username}" 
    			password="${tomcat.password}"/>
    		<java jar="${tomcat.home}/bin/bootstrap.jar" fork="true">
    			<jvmarg value="-Dcatalina.home=${tomcat.home}"/>
    			<arg line="stop"/>
    		</java>
    	</sequential>
    </parallel>
    
  73. Peter Thomas says:

    @B.Doyle – thanks for the comment, I just fixed the XML code that was lost when you posted initially.

  74. Brian Krebs says:

    This no longer works in Tomcat 7. The default Tomcat logging mechanism, JULI, is no longer in bootstrap.jar’s Manifest classpath. Therefore, it must be added manually. In order to do that, we must override the Manifest by calling the main class directly. This is the code I ended up with after also examining Peter Thomas’ excellent article on Jetty.

    
    <path id="tomcat.class.path">
    	<fileset dir="${tomcat.home}/lib">
    		<include name="**/*.jar"/>
    		<include name="**/*.zip"/>
    	</fileset>
    	<pathelement location="${tomcat.home}/bin/bootstrap.jar"/>
    	<pathelement location="${tomcat.home}/bin/tomcat-juli.jar"/>
    </path>
    
    <target name="deploy" depends="war">
    	<sequential>
    		<antcall target="tomcat-stop"/>
    		<delete dir="${tomcat.home}/webapps/${dist.context}" failonerror="false"/>
    		<copy file="${dist.dir}/${dist.filename}" todir="${tomcat.home}/webapps"/>
    		<antcall target="tomcat-start"/>
    	</sequential>
    </target>
    
    <target name="tomcat-start">
    	<java classname="org.apache.catalina.startup.Bootstrap" fork="true" 
    		classpathref="tomcat.class.path">
    		<jvmarg value="-Dcatalina.home=${tomcat.home}"/>
    	</java>
    </target>
    
    <target name="tomcat-stop" depends="tomcat-check-status" if="tomcat.started">
    	<java classname="org.apache.catalina.startup.Bootstrap" fork="true" 
    		classpathref="tomcat.class.path">
    		<jvmarg value="-Dcatalina.home=${tomcat.home}"/>
    		<arg line="stop"/>
    	</java>
    	<sleep seconds="5"/>
    </target>
    
    <target name="tomcat-check-status">
    	<condition property="tomcat.started">
    		<socket server="localhost" port="8080"/>
    	</condition>
    </target>
    
    
  75. Thangavel says:

    Hi !!

    Thanks for sharing this knowledge in the web. Good luck….

  76. Ihor says:

    I tried your first post (start tomcat from bootstrap.jar) and run into ClassNotFoundException, it was looking for class from tomcat-juli.jar. Searched the web and some guys advised to modify MANIFEST.MF of bootstrap.jar. Did so, included tomcat-juli.jar for property Class-path inside Manifest file. And it worked for me. However, I found it a bit cumbersome and decided to come back to this thread. Re-read it and … bingo! One of the last comments – appropriate solution.
    Thanks, very helpful.

  77. suren says:

    Thanks Brian ;-) it works

  78. Satheesh says:

    while am using the code the server will be started, how can i stop the server…

  79. Eugene says:

    By the way, I have endorsed libs in “${tomcat.home}/endorsed” directory in tomcat 6, so it will works with additional jvmarg:

  80. Eugene says:

    By the way, I have endorsed libs in “${tomcat.home}/endorsed” directory in tomcat 6, so it will works with additional jvmarg:


    <java jar=”${tomcat.home}/bin/bootstrap.jar” fork=”true” dir=”${tomcat.home}/webapps”$gt;
    <jvmarg value=”-Dcatalina.home=${tomcat.home}”/$gt;
    <jvmarg value=”-Djava.endorsed.dirs=${tomcat.home}/endorsed”/$gt;
    </java$gt;

  81. Eugene says:

    ha,
    I’m so bad in forum conversation. =)

    By the way, I have endorsed libs in “${tomcat.home}/endorsed” directory in tomcat 6, so it will works with additional jvmarg:



  82. Eugene says:

    Whatever…

    By the way, I have endorsed libs in “${tomcat.home}/endorsed” directory in tomcat 6, so it will works with additional jvmarg:


    <java jar=”${tomcat.home}/bin/bootstrap.jar” fork=”true” dir=”${tomcat.home}/webapps”>
    <jvmarg value=”-Dcatalina.home=${tomcat.home}”/>
    <jvmarg value=”-Djava.endorsed.dirs=${tomcat.home}/endorsed”/>
    </java>

  83. stikiflem says:

    Nice article. Btw, if some of you have problems running the tomcat. I also have an article that has a built-in tomcat you can download. That way, you won’t have to have problems with tomcat versions. It also has dt_socket argument when running the tomcat so debugging is good to go. Check it out :
    http://stikiflem.com/2011/06/how-to-run-tomcat-using-ant/

  84. vishav says:

    hi
    the script works like fire…awesome..
    i just need to know if we can keep separate consoles for ant script and tomcat i.e. the tomcat startup logs should come on a different console.?

  85. Raja says:

    The script works fine but the spawn attribute does not work for me. I am using Tomcat 6.0.32 and TFS to invoke ANT file. I have my start task as

    The build.xml never ends. It keeps running. Any suggestions?

    Thanks,

  86. Raja says:

    Sorry…my script did not make it in the previous post..

  87. VRK says:

    Hello,

    As suggested here is the script that I used in build.xml for tomcat 7

    But the path from the property file for build.tomcat.dir is not reflecting instead the script is taking my workspace path. Any clue on how to resolve this?

    Thanks

  88. VRK says:

    sorry here are the scripts:

    tomcat.class.path is the path id as described by Brian.

    C:\Dev\WebDev\XYZ\${build.tomcat.dir}\lib does not exist. where as my tomcat is at c:\tomcat

    Any clue why the property path is not reflecting here…… in all other tasks all the property paths are reflected

  89. sabyasachi Ghosh says:

    Great blog..Just looking for ..I want to stop appservers in tomcat but is there any way to check the running process ( PID) and if there is any running process can we kill it to make sure the app server really stops

  90. Why viewers still use to read news papers when in this technological world all is available
    on net?

  91. It’s a shame you don’t have a donate button!
    I’d certainly donate to this superb blog! I guess for now i’ll
    settle for book-marking and adding your RSS feed to my Google account.
    I look forward to new updates and will talk about this site with my Facebook group.
    Talk soon!

  92. Greetings! I’ve been following your site for a while now and finally got the courage to go ahead and give you a shout out from Kingwood Tx! Just wanted to tell you keep up the excellent job!

  93. venkat says:

    Below script works for me:

  94. venkat says:

  95. health says:

    Hello! I just wanted to ask if you ever have any trouble with hackers?
    My last blog (wordpress) was hacked and I ended up losing months of hard work due to no data
    backup. Do you have any methods to prevent hackers?

  96. kinesio says:

    Hello, this weekend is good for me, since this moment i am reading this impressive educational article here at my residence.

  97. Jessica says:

    I get pleasure from, result in I found exactly what I
    used to be looking for. You’ve ended my 4 day long hunt!
    God Bless you man. Have a great day. Bye

  98. Excellent blog here! Additionally your website loads up fast!
    What web host are you the use of? Can I get your associate hyperlink to your
    host? I wish my web site loaded up as quickly as yours lol

  99. Thank you for another magnificent article. The place else may anyone
    get that type of info in such an ideal approach of writing?

    I’ve a presentation next week, and I am at the look
    for such information.

  100. Good day! Do you use Twitter? I’d like to follow you if that would be ok.
    I’m definitely enjoying your blog and look forward to new
    updates.

  101. Howdy, i read your blog occasionally and i own a
    similar one and i was just curious if you get a lot of spam remarks?
    If so how do you stop it, any plugin or anything you can advise?
    I get so much lately it’s driving me mad so any help is very much appreciated.

  102. First of all I would like to say awesome blog! I had a
    quick question that I’d like to ask if you do not
    mind. I was interested to know how you center yourself and clear your thoughts before writing.
    I’ve had trouble clearing my mind in getting my ideas out there.
    I truly do take pleasure in writing but it just seems like the first 10 to 15 minutes are generally lost just trying to figure out how to
    begin. Any ideas or hints? Thanks!

  103. I know this web page presents quality dependent articles or reviews and additional data, is there any other site which offers such information in quality?

  104. Koss says:

    I try to stop tomcat 8 and have the error:
    [java] java.lang.NoClassDefFoundError: org/apache/juli/logging/LogFactory
    [java] at org.apache.catalina.startup.Bootstrap.(Bootstrap.java:52)
    [java] Caused by: java.lang.ClassNotFoundException: org.apache.juli.logging.LogFactory
    [java] at java.net.URLClassLoader$1.run(Unknown Source)
    [java] at java.net.URLClassLoader$1.run(Unknown Source)
    [java] at java.security.AccessController.doPrivileged(Native Method)
    [java] at java.net.URLClassLoader.findClass(Unknown Source)
    [java] at java.lang.ClassLoader.loadClass(Unknown Source)
    [java] at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    [java] at java.lang.ClassLoader.loadClass(Unknown Source)

    Help me, please

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 29 other followers

%d bloggers like this: