Preciso criar um serviço no windows. Toda vez que iniciar tera que ter permissao de administrador independente do usuario que logar, porem estou tendo problema pra criar o serviço. Estou utilizando o JavaService, porem na hora de iniciar o serviço da o seguinte errro : “O serviço testeService em computador local foi iniciado e interrompido. Alguns serviços sao interrompidos automaticamente
se nao estiverem sendo usados por outros serviços ou programas.”
O comando para criar o serviço é:
javaservice -install testeService “C:\Program Files\Java\jdk1.7.0_04\jre\bin\server\jvm.dll” -Djava.class.path=“c:\ManipulaSpool.jar” -start ManipulaSpool
Esse é meu codigo:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package manipulaspool;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author teste
*/
public class ManipulaSpool {
public static void log(String msg) throws IOException{
BufferedWriter br = new BufferedWriter(new FileWriter(new File("C:\\log.txt")));
br.write(msg);
br.close();
}
public static void iniciaSpool() throws Exception {
Process StartSpool;
StartSpool = Runtime.getRuntime().exec("net.exe start spooler ");
try {
if (StartSpool.waitFor() == 0) {
log("Spool Iniciado com sucesso.");
} else {
log("Erro Iniciar Spool");
}
} catch (InterruptedException ex) {
Logger.getLogger(ManipulaSpool.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void ParaSpool() throws IOException {
Process StopSpool;
Runtime.getRuntime().exec("net.exe stop lpdsvc ");
StopSpool = Runtime.getRuntime().exec("net.exe stop spooler ");
try {
if (StopSpool.waitFor() == 0) {
log("Spool Parado com sucesso.");
} else {
log("Erro Parar Spool");
}
} catch (InterruptedException ex) {
Logger.getLogger(ManipulaSpool.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void main(String[] args) throws Exception {
ParaSpool();
iniciaSpool();
}
}