PrintServiceLookup.lookupDefaultPrintService() 返回 null

发布于 2024-07-25 03:53:41 字数 213 浏览 6 评论 0原文

PrintServiceLookup.lookupDefaultPrintService() 返回 NULL 因为我安装了打印机并将其设置为默认打印机。

如果我在一个简单的程序中使用它,它工作得很好,但是当我尝试在基于小程序的程序中使用它时,它会返回NULL

请给我一些解决这个问题的好方法。

PrintServiceLookup.lookupDefaultPrintService() returns NULL as I have a printer installed and set to default printer.

If I am using this in a simple program it works fine, but when I try to use it in my applet-based program it returns NULL.

Please send me some good solution for this problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

乖不如嘢 2024-08-01 03:53:41

为了访问打印机(或主机上的任何资源),必须对小程序代码所在的 jar 文件进行签名,并且用户必须接受签名者作为可信方。
要对 jar 文件进行签名,请使用 jarsigner 程序,该程序是 JDK 的一部分。 Jarsigner 使用自己的密钥库,因此如果您有自己的证书,则必须先将证书导入到密钥库。 如果您没有任何其他证书来签署 jar 文件,它也可以生成证书。

可以在此处找到 jarsigner 工具的文档。

较新的 Java 运行时确实会询问用户是否允许代码访问打印机,但我发现无论答案如何,未签名的 jar 文件中的代码都无法访问资源。

In order to access the printer (or any resource on the host computer for that matter) the jar file in which the applet code resides has to be signed, and the user must accept the signer as a trusted party.
To sign the jar file, use the jarsigner program, which is part of he JDK. Jarsigner uses its own keystore, so if you have your own certificate, you must import the certificate to a keystore first. It can generate certificates as well, in the case you don't have any other certificate to sign the jar file with.

Documentation of the jarsigner tool can be found here.

Note that newer Java runtimes do ask the user if (s)he allows the code to access the printer, but I found that regardless of the answer, code in an unsigned jar file is prevented from accessing resources.

因为看清所以看轻 2024-08-01 03:53:41

此代码在 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());
}

This code is working in an signed applet in windows with 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());
}
牵你手 2024-08-01 03:53:41

您必须首先更改 java applet 的安全设置。 默认情况下,java applet 无法打印。

You must change the security settings for java applets first. By default, java applets cannot print.

孤芳又自赏 2024-08-01 03:53:41

确保 Printer.conf 定义 而不是 。 JVM 似乎只能找到这样定义的默认打印机。

此代码片段可以帮助快速验证它是否有效:

import javax.print.PrintServiceLookup;

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

Make sure that printer.conf defines <DefaultPrinter name> instead of <Printer name>. JVM seems to only find a default printer that is defined like this.

This code snippet could help out to quickly verify if it works:

import javax.print.PrintServiceLookup;

public class checkDefaultPrinter {
    public static void main(String[] args) {
        System.out.println(PrintServiceLookup.lookupDefaultPrintService());
    }
}
秋凉 2024-08-01 03:53:41

在 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 /浏览/JDK-8178715

In windows, make sure the Print Spooler service is running. Also, You can test this code while applying the solution

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!"); 
    } 
} 

Reference: https://bugs.openjdk.org/browse/JDK-8178715

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文