PrintServiceLookup.lookupDefaultPrintService () returns null

PrintServiceLookup.lookupDefaultPrintService() returns NULL since I have a printer installed and a default printer installed.

If I use this in a simple program, it works fine, but when I try to use it in my applex program, it returns NULL .

Please send me a good solution to this problem.

+4
source share
4 answers

To access the printer (or any resource on the host computer), the jar file containing the applet code must 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 the JDK. Jarsigner uses its own keystore, so if you have your own certificate, you must first import the certificate into the keystore. It can also create certificates if you do not have another certificate to sign the jar file.

The jarsigner tool documentation can be found here.

Note that the newer Java runtimes ask the user if they are allowed to access the printer, but I found that regardless of the answer, the code in the unsigned jar file cannot access the resources.

+2
source

This code works in a 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()); } 
+1
source

First you need to change the security settings for Java applets. By default, java applets cannot print.

0
source

Make sure printer.conf defines <DefaultPrinter name> instead of <Printer name> . The JVM seems to find only the default printer, which is defined as follows.

This piece of code can help you quickly check if it works:

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

Source: https://habr.com/ru/post/1286448/


All Articles