1

I am working on project to implementing print in java on network printer, printer is not configured. I am having problem while printer service lookup always returning null.

in Java application, is there anyway to print a file on printer which not configured?

code below is not helping to detect printer.

public int printDoc(InputStream is) throws PrintException, FileNotFoundException {
    PrintService printService=PrintServiceLookup.lookupDefaultPrintService();
    DocPrintJob job = printService.createPrintJob();
    job.addPrintJobListener(new PrintJobAdapter() {
        public void printDataTransferCompleted(PrintJobEvent event){
            System.out.println("transfer complete");
        }
        public void printJobNoMoreEvents(PrintJobEvent event){
            System.out.println("received no more events");
        }
    });

      Doc doc=new SimpleDoc(is, DocFlavor.INPUT_STREAM.AUTOSENSE, null);
      // Doc doc=new SimpleDoc(fis, DocFlavor.INPUT_STREAM.JPEG, null);
      PrintRequestAttributeSet attrib=new HashPrintRequestAttributeSet();
      attrib.add(new Copies(1));
      String printeraddr= "ipp://blrprt01.blr.network18.com/23Flr_Printer1";
      PrinterName prName =new PrinterName(printeraddr, null);

      attrib.add(prName);
      job.print(doc, attrib);
    return 0;
}
Pandey Amit
  • 657
  • 6
  • 19
  • Where's your code? – C-Otto Jan 19 '18 at 12:26
  • Would you want a program to start printing on a printer if you've never configured it? What if the destination printer had extremely expensive ink/supplies? How would you know that the program you're running generated it? This isn't a Java problem - an O/S requires it to be configured for a reason. – stdunbar Jan 21 '18 at 22:28

1 Answers1

1

You need to configure the printer first.

first line at https://docs.oracle.com/javase/tutorial/2d/printing/printable.html says selected printer

And please use PrinterJob instead of PrintServiceLookup.