title: Printing (with lpr) works from command line, but not from Java
body: <p>So, first things first, we cannot use the <code>javax</code> print services because it is extremely slow as we have more than 20,000 printers installed on a machine (the lookup uses lpstat which is incredibly slow). So, we're using lpr.</p>
  
  <p>When I do this:</p>
  
  <pre><code>cat myfile.pdf | lpr -P "MyPrinter"
  </code></pre>
  
  <p>it prints the file perfectly to the printer name <code>MyPrinter</code>. To do the same in Java, I'm doing this:</p>
  
  <pre><code>cmd = String.format("lpr -P \"%s\"", "MyPrinter");
  
  Process p = Runtime.getRuntime().exec(cmd);
  
  OutputStream out = p.getOutputStream();
  
  /*
   This essentially runs a thread which reads from a stream and
   outputs it to the STDOUT. This is what I've written to help with
   debugging
  */
  StreamRedirect inRed = new StreamRedirect(p.getInputStream(), "IN");
  StreamRedirect erRed = new StreamRedirect(p.getErrorStream(), "ER");
  
  inRed.start();
  erRed.start();
  
  
  /*
   This is where I write to lprs STDIN. `document` is an InputStream
  */
  
  final byte buf[] = new byte[1024];
  int len;
  
  while((len = document.read(buf)) &gt; 0) {
      out.write(buf, 0, len);
  }
  
  out.flush();
  out.close();
  </code></pre>
  
  <p>But, I get the following error:</p>
  
  <pre><code>SR[ER]&gt;&gt;lpr: The printer or class was not found.
  </code></pre>
  
  <p>Here, <code>SR[ER]</code> is just a custom label that is prefixed by the <code>StreamRedirect</code>. Why is this happening? Why is it able to find a printer when I run it from my command line, but not otherwise?</p>
  
  <p>Also, I tried to run <code>whoami</code> from within the Java program and it says that I am running it as the same user I am logged in as (the same user with which I execute <code>lpr</code> on the command line).</p>
  
  <p>Any help?</p>