I'm trying to create a windows service using Apache Common Daemons but I'm not able to make it work as it is meant to.
Using jvm mode it always returns error 1067 with no further explanations. Here is procrun log:
[2020-11-18 15:56:20] [info] [11104] Apache Commons Daemon procrun (1.2.3.0 32-bit) started.
[2020-11-18 15:56:20] [info] [11104] Running Service 'ServiceTest14'...
[2020-11-18 15:56:20] [info] [14684] Starting service...
Since I need RXTX and I didn't found a working 64bit implementation (that's another story) i'm forced to use a 32bit jre:
C:\ServiceTest>java -version
java version "1.8.0_271"
Java(TM) SE Runtime Environment (build 1.8.0_271-b09)
Java HotSpot(TM) Client VM (build 25.271-b09, mixed mode, sharing)
So I tried both procrun versions: 64 and 32 bits. With no difference. In both cases the windows event logger reports a crash with no useful information, at least not to me.
[2020-11-18 16:52:53] [info] [20248] Apache Commons Daemon procrun (1.2.3.0 64-bit) started.
[2020-11-18 16:52:53] [info] [20248] Running Service 'ServiceTest14'...
[2020-11-18 16:52:53] [info] [20000] Starting service...
Here follows the procrun config/setup script:
set SERVICE_NAME=ServiceTest14
set SERVICE_PATH=C:\ServiceTest
set JAR=ServiceTest2.jar
set SERVICE_CLASS=it.stikez.servicetest2.ServiceTest2
set PR_INSTALL=%SERVICE_PATH%\prunsrv.exe
sc stop %SERVICE_NAME%
sc delete %SERVICE_NAME%
REM Service log configuration
set PR_LOGPREFIX=%SERVICE_NAME%
set PR_STDOUTPUT=%SERVICE_PATH%\logs\stdout.txt
set PR_STDERROR=%SERVICE_PATH%\logs\stderr.txt
REM Path to java installation
set PR_CLASSPATH=.;%SERVICE_PATH%\%JAR%;%SERVICE_PATH%\lib\*
REM JVM configuration
set PR_JVMMS=256
set PR_JVMMX=1024
REM Install service
%SERVICE_PATH%\prunsrv.exe //IS//%SERVICE_NAME% ^
--Description="Test Service 7" ^
--Install=%PR_INSTALL% ^
--Startup=auto ^
--Jvm="C:\Program Files (x86)\Java\jre1.8.0_271\bin\client\jvm.dll" ^
--StartPath=%SERVICE_PATH% ^
--StartMode=jvm ^
--StartClass=%SERVICE_CLASS% ^
--StartMethod=main ^
--StartParams=start ^
--StopPath=%SERVICE_PATH% ^
--StopMode=jvm ^
--StopClass=%SERVICE_CLASS% ^
--StopMethod=main ^
--StopParams=stop ^
--LogPath=%SERVICE_PATH%\logs ^
--LogLevel=Trace
Setting jvm value to auto or to some nonsense gibberish (let's say random chars) makes no difference too. But at least if java mode is used (instead of jvm) it starts. At least. The Stop action doesn't work; I suppose it's spawned in a new jvm session because the control variable isn't influenced by the stop call.
Here follows the sample of java code used (which is taken directly from Apache site with some modifications since I do not need Linux portability.
package it.stikez.servicetest2;
public class ServiceTest2 {
private static ServiceTest2 engineLauncherInstance = new ServiceTest2();
private static boolean stop = false;
public static void main(String[] args) {
String cmd = "start";
if (args.length > 0) {
cmd = args[0];
System.out.println("Service called with param: " + cmd);
}
if ("start".equals(cmd)) {
engineLauncherInstance.windowsStart();
} else {
engineLauncherInstance.windowsStop();
}
}
public void windowsStart() {
System.out.println("windowsStart called");
initialize();
while (!stop) {
System.out.println("nloop stop=" + stop);
// don't return until stopped
synchronized (this) {
try {
this.wait(60000); // wait 1 minute and check if stopped
} catch (InterruptedException ie) {
System.out.println("interrupted; stop=" + stop);
}
}
}
System.out.println("Service end");
}
public void windowsStop() {
System.out.println("windowsStop called (stop=" + stop + ")");
terminate();
synchronized (this) {
System.out.println("stop sync area");
// stop the start loop
this.notify();
}
System.out.println("stop call elaborated (stop=" + stop + ")");
}
private void initialize() {
}
public void terminate() {
stop = true;
}
}
I'm (desperately) looking for a way to make it work in jvm mode OR any other magic trick to properly stop the service when the user want to.