You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Mike O'Leary <tm...@uw.edu> on 2012/07/16 00:35:31 UTC
Calling Bootstrap.main from a Java program
I would like to be able to start and stop Tomcat from a Java program running on a Windows machine. I looked at the contents of startup.bat, shutdown.bat and catalina.bat, and it looks like one easy way to do that would be to set system properties that correspond to the -D command line arguments as they are set up in the script files, and then call Bootstrap.main with a list argument that contains either "start" or "stop". When I tried this running my Java program in Eclipse, the call to Bootstrap.main(new String[]{"start"}) did not return. I don't understand why this is, because running startup.bat in a command prompt window gets Tomcat running and then returns to a command prompt. Is there a way to call Bootstrap.main(new String[]{"start"}) in an application's main thread so that it returns and Tomcat continues running?
I also tried calling Bootstrap.main(new String[]{"start"}) in a separate thread, but I must not have done it right, because when I call Bootstrap.main(new String[]{"stop"}) it claims to stop Tomcat but there is still a process listening on the port that I designated.
What is the best way to start and stop Tomcat programmatically from a Java application? If it is better to do it using a different class, such as Tomcat or Embedded, could someone point me to information about how to do that? I tried using the Tomcat class recently and I couldn't get it to work either.
I am working with Tomcat 7.0.25 x64 on Windows 2008 Server.
Thanks,
Mike
RE: Calling Bootstrap.main from a Java program
Posted by Mike O'Leary <tm...@uw.edu>.
Thanks, for this information. It helped me with everything I needed to know, including things I didn't realize that I needed to know at the time.
Mike
-----Original Message-----
From: Caldarale, Charles R [mailto:Chuck.Caldarale@unisys.com]
Sent: Sunday, July 15, 2012 4:55 PM
To: Tomcat Users List
Subject: RE: Calling Bootstrap.main from a Java program
> From: Mike O'Leary [mailto:tmoleary@uw.edu]
> Subject: Calling Bootstrap.main from a Java program
> When I tried this running my Java program in Eclipse, the call to
> Bootstrap.main(new String[]{"start"}) did not return.
The thread that calls Bootstrap.main() ends up waiting for input on the shutdown port - which a thread dump would easily show you.
> running startup.bat in a command prompt window gets Tomcat running and
> then returns to a command prompt.
You appear to have missed that startup.bat kicks off catalina.bat in a separate process.
> Is there a way to call Bootstrap.main(new String[]{"start"}) in an
> application's main thread so that it returns and Tomcat continues
> running?
You shouldn't really be doing it that way, but disabling the shutdown port in your server.xml might work.
> What is the best way to start and stop Tomcat programmatically from a
> Java application? If it is better to do it using a different class,
> such as Tomcat or Embedded, could someone point me to information
> about how to do that?
Use the Tomcat class; Embedded is deprecated. Read the Javadoc for org.apache.catalina.startup.Tomcat. You could also try launching Tomcat in a separate process, rather than inside the JVM you're already using.
> I tried using the Tomcat class recently and I couldn't get it to work
> either.
Can't provide help without specifics.
- Chuck
THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
RE: Calling Bootstrap.main from a Java program
Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: Mike O'Leary [mailto:tmoleary@uw.edu]
> Subject: Calling Bootstrap.main from a Java program
> When I tried this running my Java program in Eclipse, the call
> to Bootstrap.main(new String[]{"start"}) did not return.
The thread that calls Bootstrap.main() ends up waiting for input on the shutdown port - which a thread dump would easily show you.
> running startup.bat in a command prompt window gets Tomcat
> running and then returns to a command prompt.
You appear to have missed that startup.bat kicks off catalina.bat in a separate process.
> Is there a way to call Bootstrap.main(new String[]{"start"})
> in an application's main thread so that it returns and Tomcat
> continues running?
You shouldn't really be doing it that way, but disabling the shutdown port in your server.xml might work.
> What is the best way to start and stop Tomcat programmatically from
> a Java application? If it is better to do it using a different class,
> such as Tomcat or Embedded, could someone point me to information
> about how to do that?
Use the Tomcat class; Embedded is deprecated. Read the Javadoc for org.apache.catalina.startup.Tomcat. You could also try launching Tomcat in a separate process, rather than inside the JVM you're already using.
> I tried using the Tomcat class recently and I couldn't get it to
> work either.
Can't provide help without specifics.
- Chuck
THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org