java 打印服务查找,lookupDefaultPrintService()返回空值

7vhp5slm  于 2023-01-11  发布在  Java
关注(0)|答案(5)|浏览(127)

PrintServiceLookup.lookupDefaultPrintService()返回NULL,因为我安装了打印机并设置为默认打印机。
如果我在一个简单的程序中使用它,它工作正常,但是当我尝试在基于applet的程序中使用它时,它返回NULL
请给我一些解决这个问题的好办法。

r7s23pms

r7s23pms1#

要访问打印机(或主机上的任何资源)必须对小程序代码所驻留的jar文件进行签名,用户必须接受签名者作为受信任方。要对jar文件进行签名,请使用jarsigner程序,它是JDK的一部分。Jarsigner使用自己的密钥库,因此如果您有自己的证书,你必须先将证书导入到密钥库中。2如果你没有其他证书来签署jar文件,它也可以生成证书。
Documentation of the jarsigner tool can be found here.
请注意,较新的Java运行时确实会询问用户是否允许代码访问打印机,但我发现,不管答案如何,未签名的jar文件中的代码都无法访问资源。

wgx48brx

wgx48brx2#

此代码在Windows 1.7.0_55中的签名小程序中有效:

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;

...

HashDocAttributeSet docAttr=new HashDocAttributeSet();
HashPrintRequestAttributeSet reqAttr=new HashPrintRequestAttributeSet();

try {
    PrintService pserv = PrintServiceLookup.lookupDefaultPrintService();
    if (pserv == null) {
        System.out.println("ERROR-01: no default print service");
    }
    System.out.println("Printer: " + pserv.getName());

    DocPrintJob job = pserv.createPrintJob();
    DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
    String content = makeZplLabel();
    Doc doc = new SimpleDoc(content.getBytes(), flavor, docAttr);

    job.print(doc, reqAttr);

} catch (Exception e) {
    System.out.println("ERROR-02:" + e.getMessage());
}
nsc4cvqm

nsc4cvqm3#

必须先更改java小程序的安全设置。默认情况下,java小程序无法打印。

xtfmy6hx

xtfmy6hx4#

确保printer.conf定义<DefaultPrinter name>而不是<Printer name>。JVM似乎只找到这样定义的默认打印机。
以下代码片段有助于快速验证它是否有效:

import javax.print.PrintServiceLookup;

public class checkDefaultPrinter {
    public static void main(String[] args) {
        System.out.println(PrintServiceLookup.lookupDefaultPrintService());
    }
}
nqwrtyyt

nqwrtyyt5#

在Windows中,确保后台打印程序服务正在运行。另外,您可以在应用解决方案时测试此代码

import javax.print.PrintService; 
import javax.print.PrintServiceLookup; 

public class JavaPrintBug { 

    public static void main(String[] args) { 
        // returns an empty array unless a user has opened system printer settings 
        // only short time ago or an adminstrator has set cupsctl WebInterface=yes 
        PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null); 
        System.out.println("Number of printers available: " + printServices.length); // zero 
         
         
        // returns null unless a user has opened system printer settings only 
        // short time ago or an adminstrator has set cupsctl WebInterface=yes 
        PrintService defaultPrintService = PrintServiceLookup.lookupDefaultPrintService(); 
        if(null != defaultPrintService) { // will not be entered 
            System.out.println("Default printer: " + defaultPrintService.getName()); 
        } 
         
        // before entering this loop make sure that NO user has opened system 
        // printer settings only short time ago and NO administrator has set 
        // cupsctl WebInterface=yes (hWebInterface=no)! 
        while(null == PrintServiceLookup.lookupDefaultPrintService()) { 
            // while trapped in here, open system printer settings or set 
            // cupsctl WebInterface=yes and the loop will be left. 
            // In Windows, enable the Print Spooler service
        } 
        System.out.println("Workaround worked!"); 
    } 
}

参考:https://bugs.openjdk.org/browse/JDK-8178715

相关问题