我正在尝试将一个简单的 java jar 作为 Windows 服务运行。创建服务并尝试启动它后,我收到以下错误:
ServiceStart 返回 5。目录名无效。
完整日志:
[2019-09-27 14:24:46] [debug] ( prunsrv.c:1441) [14148] Inside ServiceMain...
[2019-09-27 14:24:46] [debug] ( prunsrv.c:904 ) [14148] reportServiceStatusE: dwCurrentState = 2, dwWin32ExitCode = 0, dwWaitHint = 3000, dwServiceSpecificExitCode = 0.
[2019-09-27 14:24:46] [info] ( prunsrv.c:1196) [14148] Starting service...
[2019-09-27 14:24:46] [debug] (rprocess.c:519 ) [14148] Apache Commons Daemon apxProcessExecute()
[2019-09-27 14:24:46] [debug] (rprocess.c:258 ) [14148] Apache Commons Daemon procrun __apxProcCreateChildPipes()
[2019-09-27 14:24:46] [debug] (rprocess.c:535 ) [14148] Apache Commons Daemon AplZeroMemory()
[2019-09-27 14:24:46] [debug] (rprocess.c:550 ) [14148] Apache Commons Daemon GetEnvironmentStringsW()
[2019-09-27 14:24:46] [debug] (rprocess.c:572 ) [14148] Apache Commons Daemon CreateProcessW()
[2019-09-27 14:24:46] [debug] (rprocess.c:616 ) [14148] Apache Commons Daemon apxProcessExecute() returning FALSE
[2019-09-27 14:24:46] [error] ( prunsrv.c:1317) [14148] Failed to execute process.
[2019-09-27 14:24:46] [error] ( prunsrv.c:1317) [14148] The directory name is invalid.
[2019-09-27 14:24:46] [error] ( prunsrv.c:1604) [14148] ServiceStart returned 5.
[2019-09-27 14:24:46] [error] ( prunsrv.c:1604) [14148] The directory name is invalid.
[2019-09-27 14:24:46] [debug] ( prunsrv.c:904 ) [14148] reportServiceStatusE: dwCurrentState = 1, dwWin32ExitCode = 1066, dwWaitHint = 0, dwServiceSpecificExitCode = 5.
[2019-09-27 14:24:46] [info] ( prunsrv.c:1670) [ 7252] Run service finished.
[2019-09-27 14:24:46] [info] ( prunsrv.c:1839) [ 7252] Apache Commons Daemon procrun finished.
即使我以管理员身份运行命令,Windows 事件查看器也会显示以下内容:
The DaemonService service terminated with the following service-specific error:
Access is denied.
用于创建服务的命令:
开始.bat
set JVM_DLL="C:\Program Files (x86)\Java\jre1.8.0_211\bin\client\jvm.dll"
set JAVA_H="C:\Program Files (x86)\Java\jre1.8.0_211\bin"
prunsrv.exe //IS//DaemonService --Description="Agent Demon Service" --Classpath="D:\procrun\daemon.jar" --StartMode=java --StartClass=com.oracle.TestDaemon --StartParams=start --StdOutput=auto --StdError=auto --JavaHome=%JAVA_H% --StartMethod=start --Jvm=%JVM_DLL% --StopMode=java --StopMethod=stop --StopParams=stop --StopClass=com.oracle.TestDaemon --StdOutput=D:\procrun\logs\stdOut.log --StdError=D:\procrun\logs\stdErr.log --LogLevel=Debug --LogPath=D:\procrun\logs --LogPrefix=com-daemon
我在 daemon.jar 中有一个非常简单的代码,如下所示,如果使用 java -jar 执行,jar 可以工作:
package com.oracle;
public class TestDaemon {
private static boolean stop = false;
public static void start(String[] args) {
System.out.println("start");
try{
Thread.sleep(5000);
}catch (InterruptedException e) {
e.printStackTrace();
}
while (!stop) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void stop(String[] args) {
System.out.println("stop");
stop = true;
System.exit(0);
}
public static void main(String[] args) {
System.out.println("Executing main");
if(args== null || args.length == 0){
System.out.println("Args is null");
args = new String[1];
args[0] = "start";
}
if ("start".equals(args[0])) {
start(args);
} else if ("stop".equals(args[0])) {
stop(args);
}
}
}
'''