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

Java Printing: Default print service lookup problems when running CUPS

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • 9
    • 1.4.2
    • client-libs
    • 2d
    • x86
    • linux

      FULL PRODUCT VERSION :
      java version "1.4.2"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2-b28)
      Java HotSpot(TM) Client VM (build 1.4.2-b28, mixed mode)

      FULL OS VERSION :
      Linux frogstar 2.4.19-64GB-SMP #1 SMP Thu Mar 20 15:42:37 UTC 2003 i686 unknown

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      SuSE 8.1
      glibc 2.2.5
      CUPS 1.1.15

      A DESCRIPTION OF THE PROBLEM :
      PrintServiceLookup.lookupPrintServices() does not return a useful
      value under Linux. There are several possibilities for identifying
      the default printer on a Unix/Linux system:

          $PRINTER
          $LPDEST
          lpstat -d

      lookupPrintServices() doesn't seem to use any of these. Instead
      it returns the first configured printer on the system, taken in
      alphabetical order. If this happens to be the current user's
      default printer, it's only by coincidence.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run test program included below.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The value of $PRINTER, $LPDEST or `lpstat -d`.
      ACTUAL -
      The first configured printer on the system,
      taken in alphabetical order, equivalent to:

          grep -v ^# /etc/printcap | sort | head -1

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import javax.print.*;

      public class Printer {
          public static void main( String args[] ) {
              PrintService printer = PrintServiceLookup.lookupDefaultPrintService();
              System.out.println( printer.getName() );
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      PrintService[] services = null;
      PrintService myPrinter = null;
      DocFlavor myFlavor = DocFlavor.INPUT_STREAM.POSTSCRIPT;
      PrintRequestAttributeSet jobAttrs = new HashPrintRequestAttributeSet();
      services = PrintServiceLookup.lookupPrintServices( myFlavor, jobAttrs );
      if ( System.getProperty( "os.name" ).startsWith( "Windows" ) )
          // No problem under Windows ...
          myPrinter = PrintServiceLookup.lookupDefaultPrintService();
      else {
          try { // XXX Yuck! XXX
              Process child = Runtime.getRuntime().exec(
                  new String[] { "/bin/sh", "-c", "echo ${PRINTER:-$LPDEST}" }
              );
              BufferedReader rdr = new BufferedReader(
                  new InputStreamReader( child.getInputStream() )
              );
              String envPrinter = rdr.readLine().trim();
              child.destroy();
              for ( int i=0; myPrinter == null && i<services.length; i++ )
                  if ( services[i].getName().equals( envPrinter ) )
                      myPrinter = services[i];
          }
          catch ( Exception ignore ) {}
      }
      if ( myPrinter == null )
          myPrinter = services[0];
      ###@###.### 10/19/04 21:30 GMT

            psadhukhan Prasanta Sadhukhan
            prr Philip Race
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: