【问题标题】:Win Scheduled Task cannot see PrintService赢得计划任务看不到 PrintService
【发布时间】:2015-05-14 10:56:30
【问题描述】:

我有一个将 PDF 文件发送到打印机的 java 代码。 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运行但java返回错误:

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

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

谁能给我一些建议,看看这里可能存在什么问题?

编辑: 该行发生异常:

printJob.setPrintService(myService);

【问题讨论】:

    标签: java windows printing scheduled-tasks scheduler


    【解决方案1】:

    解决此问题的方法是将操作系统上现有的 java 从版本 6u45 升级到 7u21。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-19
      • 2011-05-25
      • 2011-05-22
      • 2011-01-22
      相关资源
      最近更新 更多