JDK-6664915 : SecurityException using javax.print APIs when queuePrintJob permission is granted.
  • Type: Bug
  • Component: client-libs
  • Sub-Component: 2d
  • Affected Version: 5.0
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2008-02-19
  • Updated: 2011-03-07
  • Resolved: 2011-03-07
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 6 JDK 7
6u10Fixed 7 b28Fixed
Description
In some circumstances, when

- printing on windows using the javax.print APIs,
- and a security manager is present
- and queuePrintJob permission is granted,

then a SecurityException may still be thrown.

Running the program below demonstrates this

java -Djava.security.manager=default -Djava.security.policy=print.policy

// print.policy 
grant {
  permission java.lang.RuntimePermission "queuePrintJob";
};


// PrintSE.java
import javax.print.attribute.*;

public class PrintSE implements Printable {

    public static void main(String[] args) throws Exception {
        GraphicsEnvironment.getLocalGraphicsEnvironment();

        PrintService service = PrintServiceLookup.lookupDefaultPrintService();
        if (service == null) {
            return;
        }
        SimpleDoc doc =
             new SimpleDoc(new PrintSE(),
                           DocFlavor.SERVICE_FORMATTED.PRINTABLE,
                           new HashDocAttributeSet());
        DocPrintJob job = service.createPrintJob();
        job.print(doc, new HashPrintRequestAttributeSet());
    }

    public int print(Graphics g, PageFormat pf, int pg) {
       if (pg > 0) return NO_SUCH_PAGE;
       g.drawString("Test passes.", 100, 100);
       return PAGE_EXISTS;
   }
}

Comments
WORK AROUND 1. The exception occurs only the first time. The app can swallow it and re-try 2. This is specific to javax.print APIs, PrinterJob can be used instead. 3. Since to encounter this queuePrintJob permission must be granted, then it may be possible to grant file read too.
19-02-2008

EVALUATION The exception comes when listing fonts in the jre/lib/font directory in order to register these fonts with GDI, so that GDI can use these fonts directly when printing. The exception is specific to particular circumstances. See workaround section for workarounds
19-02-2008