Hi guys,
I have written a simple windows service. It only creates a thread and that
thread logs a random UUID to a file every 1 second.
public class WindowsService {
private static MyRunnable r;
private static Thread t;
public static void main(String[] args) {
r = new MyRunnable();
t = new Thread(r);
t.start();
}
public static void start(String[] args) {
main(args);
}
public static void stop(String[] args) {
r.setStopped(true);
try {
t.join();
} catch (InterruptedException ex) { /* do nothing */ }
}
}
public class MyRunnable implements Runnable {
private boolean isStopped;
private FileHandler handler;
public MyRunnable() {
try {
handler = new FileHandler("C:\\access.log", 100000, 5, true);
} catch (IOException | SecurityException ex) { /* do nothing */}
isStopped = false;
}
@Override
public void run() {
handler.publish(new LogRecord(Level.INFO, "starting service"));
while (!isStopped) {
handler.publish(new LogRecord(Level.INFO,
UUID.randomUUID().toString()));
try {
Thread.sleep(1000L);
} catch (InterruptedException ex) {
isStopped = true;
}
}
handler.publish(new LogRecord(Level.INFO, "exiting service"));
}
public synchronized void setStopped(boolean isStopped) {
this.isStopped = isStopped;
}
}
I add this service with this command:
prunsrv.exe //IS//MyService --DisplayName "Log Service" --Jvm auto --StartM
ode jvm --StopMode jvm --Classpath "My jar file path" --StartClass win
dowsservice.WindowsService --StopClass windowsservice.WindowsService --StartMeth
od start --StopMethod stop
I can start and stop the service and I can see "starting service" and "exiting
service" at the log file. But when I stop the service I got this error:
Commons Daemon Service Runner has encountered a problem and needs to close. We
are sorry for the inconvenience.
in the Event Viewer i see this message:
Faulting application prunsrv.exe, version 1.0.10.0, faulting module jvm.dll,
version 23.3.0.1, fault address 0x0012baba.
Why is this happening? What is wrong with this program?