title: Printing via cups using javax.print
body: <p>I have a file that works just fine if I use the command <code>lp filename</code>.</p>
  
  <p>The file is an ESC/P file for a receipt impact printer. That has linux native CUPS drivers and all that works. </p>
  
  <p>Im trying to use the <code>javax.print</code> API so that I can have a finer grained control over the printing and hopefully keep it cross-platform compatible, though Linux is the target platform.</p>
  
  <p>I've tried every DocFlavor combination known to man and every type of data type (<code>InputStream</code>, <code>byte[]</code>, <code>Reader</code> etc.) </p>
  
  <p>It either just ignores the <code>print()</code> command all together or just flips out a blank sheet of paper. Running <code>lp filename</code> prints it perfect, so how do I get <code>javax.print</code> to just do the functional equivalent of <code>lp filename</code>?</p>
  
  <p>I'm not set on using <code>javax.print</code> I can use other "things" and might start investigating cups4J but it seems it would limit me to Linux/*nix only, which is OK for now but would rather have a cross platform solution. </p>
  
  <p>I could just issue the <code>lp</code> system command on the file but, I need finer grained control. These aren't receipts we're printing, they are tickets and the tickets range from $5.00 to to thousands of dollars. Currently if we detect a printing issue, we void the transaction and if anything printed, its invalid, we don't take reprints lightly and most of the time charge to print a new copy if the customer looses his copy. Oh the reason for doing this is we're changing the POS system from Windows to Linux and the printers from direct access over serial ports to CUPS managed over USB. Here's my code that doesn't work. Any help is appreciated. </p>
  
  <pre><code>try {
      // Find the default service
      DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
  
      PrintService service = PrintServiceLookup.lookupDefaultPrintService();
  
      // Create the print job
      DocPrintJob job = service.createPrintJob();
      InputStream in = new FileInputStream("/home/bart/real.escp");
      Doc docNew = new SimpleDoc(in,flavor,null);
  
      // Monitor print job events; for the implementation of PrintJobWatcher,
      // see Determining When a Print Job Has Finished
      PrintJobWatcher pjDone = new PrintJobWatcher(job);
  
      // Print it
      job.print(docNew, null);
  
      // Wait for the print job to be done
      pjDone.waitForDone();
  
      // It is now safe to close the input stream
      in.close();
  } catch (PrintException e) {
  } catch (IOException e) {
  }
  </code></pre>