To start and stop tomcat server using Java
The command to execute in dos prompt - For Tomcat 5.5
To Start: net start tomcat5
To Stop: net stop tomcat5
public class TestTomcatService
{
public static void main(String[] args) throws IOException
{
TestTomcatService obj = new TestTomcatService();
obj.startService();
obj.stopService();
}
public void startService()
{
try
{
String[] command = new String[3];
command[0] = "cmd";
command[1] = "/C";
command[2] = "net start tomcat5";
Process p = Runtime.getRuntime().exec(command);
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
// read the output from the command
String s = null;
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
public void stopService()
{
try
{
String[] command = new String[3];
command[0] = "cmd";
command[1] = "/C";
command[2] = "net stop tomcat5";
Process p = Runtime.getRuntime().exec(command);
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
// read the output from the command
String s = null;
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
To Start: net start tomcat5
To Stop: net stop tomcat5
public class TestTomcatService
{
public static void main(String[] args) throws IOException
{
TestTomcatService obj = new TestTomcatService();
obj.startService();
obj.stopService();
}
public void startService()
{
try
{
String[] command = new String[3];
command[0] = "cmd";
command[1] = "/C";
command[2] = "net start tomcat5";
Process p = Runtime.getRuntime().exec(command);
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
// read the output from the command
String s = null;
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
public void stopService()
{
try
{
String[] command = new String[3];
command[0] = "cmd";
command[1] = "/C";
command[2] = "net stop tomcat5";
Process p = Runtime.getRuntime().exec(command);
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
// read the output from the command
String s = null;
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
Comments
Post a Comment