java-native-access/jna · error · RuntimeException

Unable to start the service

Error message

Unable to start the service

What it means

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.

Solutions

  1. Check queryStatus() and the Windows Event Viewer for the service's actual failure reason
  2. Verify the service's dependencies (SERVICE_DEPENDENCIES) are started before calling startService
  3. Check the service's Log On account credentials and executable path in services.msc
  4. Catch the RuntimeException and poll queryStatus().dwCurrentState with a longer custom timeout before giving up

Example fix

// before
service.startService();
// after
try {
    service.startService();
} catch (RuntimeException e) {
    Winsvc.SERVICE_STATUS st = service.queryStatus();
    if (st.dwCurrentState == Winsvc.SERVICE_STOPPED) {
        throw new IllegalStateException("Service failed to start; check Event Viewer", e);
    }
}
Defensive patterns

Strategy: try-catch

Validate before calling

Winsvc.SERVICE_STATUS st = service.queryStatus();
boolean startable = st.dwCurrentState == Winsvc.SERVICE_STOPPED
        && service.getLAUNCH_PROTECTION() == null; // plus dependency checks per service config

Try / catch

try {
    service.startService();
} catch (RuntimeException e) {
    Winsvc.SERVICE_STATUS st = service.queryStatus();
    if (st.dwCurrentState != Winsvc.SERVICE_RUNNING) {
        // inspect Event Viewer / dwWin32ExitCode, then retry with custom timeout
    }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of java-native-access/jna@d036ad9781 (2026-09-12). Data as JSON: /api/errors/32f4a2f3af088f64. Report an issue: GitHub.

Appendix: source

Thrown at contrib/platform/src/com/sun/jna/platform/win32/W32Service.java:210

                status, status.size(), size)) {
            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
        }

        return status;
    }

    public void startService() {
        waitForNonPendingState();
        // If the service is already running - return
        if (queryStatus().dwCurrentState == Winsvc.SERVICE_RUNNING) {
            return;
        }
        if (!Advapi32.INSTANCE.StartService(_handle, 0, null)) {
            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
        }
        waitForNonPendingState();
        if (queryStatus().dwCurrentState != Winsvc.SERVICE_RUNNING) {
            throw new RuntimeException("Unable to start the service");
        }
    }

    /**
     * Stop service.
     */
    public void stopService() {
        stopService(30000);
    }

    /**
     * Stop service.
     *
     * @param timeout timeout in ms until the service must report to be stopped
     */
    public void stopService(long timeout) {
        long startTime = System.currentTimeMillis();
        waitForNonPendingState();

View on GitHub (pinned to d036ad9781)