Tuesday, March 18, 2014

Starting Windows Services from the java code(Tomcat)

in order to start a particular service you need to confirm that the service is there in the services Tab of the Window Task Manager.Here i will be starting tomcat.

Copy and paste the following code

package General;

import java.io.*;
import java.util.*;

public class Service {
  public static void main(String args[]) {

 String arg1="start";
 String arg2="stop";
    String[] command = {"cmd.exe", "/c", "sc", arg1, "Tomcat7"};
    try {
      Process process = new ProcessBuilder(command).start();
      InputStream inputStream = process.getInputStream();
      InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
      BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
      String line;
      while ((line = bufferedReader.readLine()) != null) {
         System.out.println(line);
      }
    } catch(Exception ex) {
      System.out.println("Exception : "+ex);
    }
  }
}


No comments:

Post a Comment

java-8-streams-map-examples

package com.mkyong.java8; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Arrays; import java.util.List; im...