{"record":{"id":"4841098d70d7d2db","repo":"java-native-access/jna","slug":"failed-to-access-the-print-server-errorcode","errorCode":null,"errorMessage":"Failed to access the print server - <errorCode>","messagePattern":"Failed to access the print server - <errorCode>","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"contrib/w32printing/src/com/sun/jna/platform/win32/Win32SpoolMonitor.java","lineNumber":104,"sourceCode":"    }\n\n    public int getLastError() {\n        int rc = Kernel32.INSTANCE.GetLastError();\n\n        if (rc != 0)\n            System.out.println(\"error: \" + rc);\n\n        return rc;\n    }\n\n    public void monitorAllPrinters() {\n        System.out.println(\"Monitoring all printers, press Ctrl + C to stop\");\n        HANDLEByReference printServerHandle = new HANDLEByReference();\n        boolean success =\n            Winspool.INSTANCE.OpenPrinter(null, printServerHandle, null);\n        if (!success) {\n            int errorCode = Kernel32.INSTANCE.GetLastError();\n            throw new RuntimeException(\"Failed to access the print server - \" +\n                errorCode);\n        }\n\n        try {\n            PRINTER_NOTIFY_OPTIONS options = new PRINTER_NOTIFY_OPTIONS();\n            options.Count = 1;\n            PRINTER_NOTIFY_OPTIONS_TYPE.ByReference optionsType =\n                new PRINTER_NOTIFY_OPTIONS_TYPE.ByReference();\n            optionsType.Type = JOB_NOTIFY_TYPE;\n            optionsType.setFields(new short[] {\n                JOB_NOTIFY_FIELD_PRINTER_NAME,\n                JOB_NOTIFY_FIELD_STATUS,\n                JOB_NOTIFY_FIELD_DOCUMENT\n            });\n            optionsType.toArray(1);\n            options.pTypes = optionsType;\n            HANDLE changeNotificationsHandle =\n                Winspool.INSTANCE.FindFirstPrinterChangeNotification(","sourceCodeStart":86,"sourceCodeEnd":122,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/contrib/w32printing/src/com/sun/jna/platform/win32/Win32SpoolMonitor.java#L86-L122","documentation":"Win32SpoolMonitor.monitorAllPrinters() opens a handle to the local print server by calling OpenPrinter with a null printer name. If OpenPrinter fails, the last error code is read and a RuntimeException is thrown with the message 'Failed to access the print server - <errorCode>'. The monitor cannot attach to the spooler at all.","triggerScenarios":"OpenPrinter(null, ...) returning FALSE, typically because the process has no permission to open the print server or the spooler service is not running.","commonSituations":"Running without administrator/elevated rights in a non-interactive session; Print Spooler service stopped; restricted service accounts lacking server-level printer access.","solutions":["Check the errorCode: 5 (ERROR_ACCESS_DENIED) means run elevated or grant the account print-server permissions; 1722/RPC errors mean the spooler is unreachable.","Start the Print Spooler service (net start spooler).","Run the monitor in an interactive session or as a service account with print-operator rights.","Catch the RuntimeException and degrade gracefully instead of crashing the demo/main flow."],"exampleFix":"// before\nWinspool.INSTANCE.OpenPrinter(null, printServerHandle, null);\n// after\nboolean success = Winspool.INSTANCE.OpenPrinter(null, printServerHandle, null);\nif (!success) {\n    int err = Kernel32.INSTANCE.GetLastError();\n    if (err == W32Errors.ERROR_ACCESS_DENIED) {\n        LOG.error(\"Run elevated or grant print-server access to this account\");\n    }\n    return;\n}","handlingStrategy":"try-catch","validationCode":"boolean canOpenPrintServer() {\n    HANDLEByReference h = new HANDLEByReference();\n    boolean ok = Winspool.INSTANCE.OpenPrinter(null, h, null);\n    if (ok) { Winspool.INSTANCE.ClosePrinter(h.getValue()); }\n    return ok;\n}","typeGuard":null,"tryCatchPattern":"try {\n    monitor.monitorAllPrinters();\n} catch (RuntimeException e) {\n    LOG.error(\"Print-server access failed: \" + e.getMessage()\n        + \" (run elevated / start spooler)\");\n}","preventionTips":["Run monitoring code elevated or with print-operator privileges.","Ensure the spooler service is running before starting the monitor.","Fail fast with a clear permission message when OpenPrinter fails.","Use OpenPrinter(null,...) as an early probe for environment issues."],"tags":["windows","printing","spooler","runtime-exception"],"backgroundTag":"api-error-response","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"}