What is the best way to start/stop/restart Apache Tomcat using Java code? If it cant be platform dependent (or complicated or I have to do a "IF" for each), then only for Windows.


I know how to do this in linux, but it might be similar in windows,

so say you have your Tomcat shell script in your init.d directory, your command should be

service Tomcat {start|stop|restart}

then to run it in java you might want something like:

String cmd = "service Tomcat {start|stop|restart}";
		Runtime run = Runtime.getRuntime();
		Process pr = run.exec(cmd);
                //from here only if you want to read the command output
		BufferedReader buf = new BufferedReader(new InputStreamReader(pr.getInputStream()));
		String line = "";
		while ((line=buf.readLine())!=null) {

so basically replace the cmd variable with whatever command you use to start your server in the command prompt or terminal