How to print a file directly on network printer in java.. Printer is not configured in system where application is running?

4.4k Views Asked by At

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;
}
1

There are 1 best solutions below

0
Siddhartha Manka On

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.