Win Scheduled Task 看不到 PrintService

Win Scheduled Task cannot see PrintService

我有一个 java 代码可以将 PDF 文件发送到打印机。 Java 代码是这样的:

import java.awt.print.PrinterJob;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.pdfbox.pdmodel.PDDocument;

public class PrintPdf {
    protected final Log logger = LogFactory.getLog(getClass());

public void print(String pdfFile, String printer, int copies) throws Exception {

    PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
    PDDocument document = null;

    try
    {
        document = PDDocument.load( pdfFile );
        PrinterJob printJob = PrinterJob.getPrinterJob();
        PrintService[] printServices = PrinterJob.lookupPrintServices();
        PrintService myService = null;

        if (printServices.length > 0)
        {
            for(PrintService service : printServices) {
                if(service.getName().toLowerCase().contains(printer.toLowerCase())) {
                    myService = service;
                    break;
                }
            }
            if (myService == null) {
                throw new Exception("Printer not found " + printer);
            } else {
                logger.info("Printer found " + myService.getName());
            }
        } else {
            throw new Exception("No print services found");
        } 

        printJob.setPrintService(myService);
        printJob.setCopies(copies);
        document.silentPrint( printJob );

    }
    finally {
        if( document != null ) {
            document.close();
        }
    }   
}

此 java 从批处理文件中调用。我已安排 windows 任务每隔 X 分钟 运行 文件。计划任务 运行 具有管理员权限的用户。所有这些都是 运行 在 Windows 2003 服务器上。 打印机使用 TCP/IP 地址设置。

问题:当用户登录后,任务 运行 可以将 PDF 文件发送到打印机。 当用户未登录时,Task 运行s but java returns error:

java.awt.print.PrinterException: Invalid name of PrintService

Java 程序成功地在循环中列出可用的打印服务,就在打印命令之前,但由于某种原因无法在用户未登录时打印文档。

任何人都可以给我一些建议吗?

编辑: 行中出现异常:

printJob.setPrintService(myService);

此问题的解决方案是将 OS 上现有的 java 从版本 6u45 升级到 7u21。