{"record":{"id":"32f4a2f3af088f64","repo":"java-native-access/jna","slug":"unable-to-start-the-service","errorCode":null,"errorMessage":"Unable to start the service","messagePattern":"Unable to start the service","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"contrib/platform/src/com/sun/jna/platform/win32/W32Service.java","lineNumber":210,"sourceCode":"                status, status.size(), size)) {\n            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());\n        }\n\n        return status;\n    }\n\n    public void startService() {\n        waitForNonPendingState();\n        // If the service is already running - return\n        if (queryStatus().dwCurrentState == Winsvc.SERVICE_RUNNING) {\n            return;\n        }\n        if (!Advapi32.INSTANCE.StartService(_handle, 0, null)) {\n            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());\n        }\n        waitForNonPendingState();\n        if (queryStatus().dwCurrentState != Winsvc.SERVICE_RUNNING) {\n            throw new RuntimeException(\"Unable to start the service\");\n        }\n    }\n\n    /**\n     * Stop service.\n     */\n    public void stopService() {\n        stopService(30000);\n    }\n\n    /**\n     * Stop service.\n     *\n     * @param timeout timeout in ms until the service must report to be stopped\n     */\n    public void stopService(long timeout) {\n        long startTime = System.currentTimeMillis();\n        waitForNonPendingState();","sourceCodeStart":192,"sourceCodeEnd":228,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/contrib/platform/src/com/sun/jna/platform/win32/W32Service.java#L192-L228","documentation":"W32Service.startService calls the Win32 StartService API and then waits for the service to leave pending states. If StartService succeeded but the service's dwCurrentState is not SERVICE_RUNNING after waiting, a RuntimeException 'Unable to start the service' is thrown — the service start attempt did not result in a running state.","triggerScenarios":"StartService succeeds but the service process fails/exits during startup, dependencies are missing or fail to start, the service's timeout to report SERVICE_RUNNING expires, or the service is configured with a wrong binary path/credentials so it crashes immediately.","commonSituations":"Services whose executable or dependencies are misconfigured; services that need credentials the caller lacks; slow-starting services exceeding waitForNonPendingState expectations; service crashes logged in the Windows Event Viewer.","solutions":["Check queryStatus() and the Windows Event Viewer for the service's actual failure reason","Verify the service's dependencies (SERVICE_DEPENDENCIES) are started before calling startService","Check the service's Log On account credentials and executable path in services.msc","Catch the RuntimeException and poll queryStatus().dwCurrentState with a longer custom timeout before giving up"],"exampleFix":"// before\nservice.startService();\n// after\ntry {\n    service.startService();\n} catch (RuntimeException e) {\n    Winsvc.SERVICE_STATUS st = service.queryStatus();\n    if (st.dwCurrentState == Winsvc.SERVICE_STOPPED) {\n        throw new IllegalStateException(\"Service failed to start; check Event Viewer\", e);\n    }\n}","handlingStrategy":"try-catch","validationCode":"Winsvc.SERVICE_STATUS st = service.queryStatus();\nboolean startable = st.dwCurrentState == Winsvc.SERVICE_STOPPED\n        && service.getLAUNCH_PROTECTION() == null; // plus dependency checks per service config","typeGuard":null,"tryCatchPattern":"try {\n    service.startService();\n} catch (RuntimeException e) {\n    Winsvc.SERVICE_STATUS st = service.queryStatus();\n    if (st.dwCurrentState != Winsvc.SERVICE_RUNNING) {\n        // inspect Event Viewer / dwWin32ExitCode, then retry with custom timeout\n    }\n}","preventionTips":["Ensure service dependencies are started first","Verify service account credentials and binary path before starting","Poll queryStatus() yourself with a generous timeout instead of relying on defaults","Check the Windows Event Log when startup fails to find the root cause"],"tags":["win32","service-control","start-service"],"backgroundTag":"service-start-failed","analyzedSha":"d036ad9781adad4b66693e8fa7098e4ac665e0a3","analyzedAt":"2026-09-12T06:50:59.239Z","contentChangedAt":"2026-09-12T06:50:59.239Z","schemaVersion":2},"datasetVersion":"2026-09-19T12:17:13.211Z"}