Bom dia,
Estou com problema para iniciar e parar serviços do Windows 10 com aplicação Java. Alguém sabe me falar como devo realizar esse procedimento?
Estou utilizando desta forma:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
public class pararStart {
// try {
// Process escrita = Runtime.getRuntime().exec("sc query
// C:\\SAJ\\Servidor\\SIGMP-Robo\\fmpRobo.exe"); //Status do Serviço de
// Indexação do windows
// System.out.println(escrita);
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
//
static final String CMD_START = "cmd /c net start \"C:\\SAJ\\Servidor\\SIGMP\\SIGSERVIDOR.exe";
static final String CMD_STOP = "cmd /c net stop \"C:\\SAJ\\Servidor\\SIGMP\\SIGSERVIDOR.exe";
public static int startService(String serviceName) throws Exception {
return execCmd(CMD_START + serviceName + "\"");
}
public static int stopService(String serviceName) throws Exception {
return execCmd(CMD_STOP + serviceName + "\"");
}
static int execCmd(String cmdLine) throws Exception {
Process process = Runtime.getRuntime().exec(cmdLine);
StreamPumper outPumper = new StreamPumper(process.getInputStream(), System.out);
StreamPumper errPumper = new StreamPumper(process.getErrorStream(), System.err);
outPumper.start();
errPumper.start();
process.waitFor();
outPumper.join();
errPumper.join();
return process.exitValue();
}
static class StreamPumper extends Thread {
private InputStream is;
private PrintStream os;
public StreamPumper(InputStream is, PrintStream os) {
this.is = is;
this.os = os;
}
public void run() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = br.readLine()) != null)
os.println(line);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
pararStart n = new pararStart();
try {
n.stopService(CMD_STOP);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}