I've gone through some of the links for this on Google. I'm also able to shut down the server through Java code but can't seem to understand how the Java code to run the server will execute when the server is shutdown. Any explanations?
-
Can you show us how you are shutting down the server through codes ? – Suresh Atta Dec 11 '15 at 08:26
-
I don't understand this: `how the Java code to run the server will execute when the server is shutdown`. Are you trying to embed a Tomcat into your application ? That means your applications launches Tomcat on its own ? – Marged Dec 11 '15 at 08:38
-
@Marged I'm supposed to implement a server start/stop functionality on my jsp page i.e. when I l click on 'Start Server' the server must start and the link must change to 'Stop Server' Now when I click on it the server must stop and the link must change to 'Start Server' and so on and so forth. I don't know where to write the standard piece of code for starting the server- in a main program or a servlet? If it is a servlet how will I run the java code if the server is down? – Cafe Dec 11 '15 at 08:44
-
As long as you don't have two Tomcat instances this won't work: how is a not-started Tomcat supposed to provide a form that allows to start Tomcat ?!? You have a [bootstrapping problem](https://en.wikipedia.org/wiki/Bootstrapping#Etymology) ... – Marged Dec 11 '15 at 08:46
-
@Marged Thank you. At least you got my question. So this solution will work only if more than one Tomcat instance is running? Also, would be grateful if you could explain what bootstrapping is or provide relevant links. – Cafe Dec 11 '15 at 08:49
-
@sᴜʀᴇsʜᴀᴛᴛᴀ This is the code and it works - try { Process child = Runtime.getRuntime().exec(lien+"/shutdown.bat"); System.out.println("Server must Shut Down"); } catch (IOException ex) { System.out.println("Exception exception"+ex); } – Cafe Dec 11 '15 at 08:50
-
Yes, if you want control Tomcat from Web you need at least a second instance for the starting process. Shutting down could (but I don't say you should :-)) be done by only one instance. The second instance does not necessarily have to be a complete Tomcat, you could create a cgi for that and run this in Apache or write a simple Spring Boot app that hosts a small form for you. – Marged Dec 11 '15 at 08:55
-
@Marged Thanks a ton buddy for the guidance. – Cafe Dec 11 '15 at 09:16
3 Answers
You can execute native commands on startup
using java:
String command = "c:\program files\tomcat\bin\startup.bat";//for linux use .sh
Process child = Runtime.getRuntime().exec(command);
In shutdown
use:
String command = "c:\program files\tomcat\bin/shutdown..bat";//for linux use .sh
Process child = Runtime.getRuntime().exec(command);

- 8,299
- 4
- 22
- 36
-
I'm unable to get this. How does one execute native commands? Also, if my server is not up how will the Java code to execute the above command work? – Cafe Dec 11 '15 at 08:28
-
-
@Manu I'm supposed to implement a server start/stop functionality on my jsp page i.e. when I l click on 'Start Server' the server must start and the link must change to 'Stop Server' Now when I click on it the server must stop and the link must change to 'Start Server' and so on and so forth. I don't know where to write the standard piece of code for starting the server- in a main program or a servlet? If it is a servlet how will I run the java code if the server is down? – Cafe Dec 11 '15 at 08:39
-
@Cafe you can do this in jsp if you like but better in sevlet adding a condition "if" when it's in start go to stop or when instop go to start when you click – Abdelhak Dec 11 '15 at 08:44
-
@Abdelhak I get this buddy. But my question is this - if the server is down i.e. it has not started how will the servlet (containing the code to run the server using java code) run in the first place? – Cafe Dec 11 '15 at 08:47
-
-
@Manu The project that I'm currently working on has these requirements. :) Is it possible for me to write the server start/stop java code in a main program and then call this java class from a jsp page? – Cafe Dec 11 '15 at 08:54
This answer is more appealing <-- Click
System.getProperty("catalina.home")
This is more promising and suggestive way of triggaring such a batch call

- 1
- 1

- 11
- 1
What you are basically trying to do is have a Tomcat bootstrap itself (impossible because this is a chicken or the egg problem) or shut itself down (possible).
I suggest you either run two instances of Tomcat on different ports or run a different component that is able to expose services via http (Apache webserver with cgi, Spring Boot based webapp, ...) One instance will solely be used for the management part and runs on a different port (or vhost if Apache webserver is involved)
The management web interface then calls the standard startup and shutdown command files provided for both Linux and Windows and either keeps track of the state or searches for the Tomcat process using ps
or tasklist
.
In case you want to start and stop applications deployed to Tomcat and not the complete Tomcat instance you can use Tomcat Manager instead.

- 10,577
- 10
- 57
- 99