Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8178715

Querying PrintServices (and default PrintService) fails on macOS

    XMLWordPrintable

    Details

    • Subcomponent:
      2d
    • CPU:
      x86
    • OS:
      os_x
    • Verification:
      Verified

      Description

      FULL PRODUCT VERSION :
      java version "1.8.0_77"
      Java(TM) SE Runtime Environment (build 1.8.0_77-b03)
      Java HotSpot(TM) 64-Bit Server VM (build 25.77-b03, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      macOS Sierra 10.12.3

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      At least one printing device (may be a virtual printer or a PDF printer) must be registered in the system.

      A DESCRIPTION OF THE PROBLEM :
      Querying PrintSercives from the system using javax.print.PrintServiceLookup will always fail unless the user opens the systems printer settings section or an administrator sets the system property cupsctl WebInterface=yes.

      PrintServiceLookup.lookupPrintServices(null, null);
      will return an empty array,

      PrintServiceLookup.lookupDefaultPrintService();
      will return null.

      The problem can be worked around at runtime but the workaround will only last for a short time.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Make sure you have at least one printer installed.
      Make sure the system property cupsctl WebInterface is set to no (cupsctl WebInterface=no);
      Boot (or reboot) macOS.
      Execute the provided source code for an executable test case, i.e. using NetBeans in debug mode.
      The static PrintServiceLookup method calls will fail as described.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The static PrintServiceLookup.lookupPrintServices method call will return an array with at least one PrintService (those PrintServices that are actually installed in the system).

      The static PrintServiceLookup.lookupDefaultPrintService method call will return a PrintService that correlates to the systems default printer.
      ACTUAL -
      The static PrintServiceLookup.lookupPrintServices method call will return an empty array.

      The static PrintServiceLookup.lookupDefaultPrintService method call will return null.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      package javaprintbug;

      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.
              }
              System.out.println("Workaround worked!");
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Opening the system printer settings will fix the problem for a short time.
      Setting cupsctl WebInterface=yes will fix the problem and prevent it from occuring again. Requires administrator privileges.
      Note: Both is not reasonable in real life customer situations.

        Attachments

          Activity

            People

            Assignee:
            pardesha Pardeep Sharma
            Reporter:
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: