Printing through java code but job always stuck in queue

Viewed 118

This is the code I am trying to execute. It works and I see the document in the printer queue, but the printer does not print the page.

Printing from other apps does not get stuck in the queue.

Any idea what the problem is?

My printer is HP Deskjet 3512 on port USB001

public static void main(String[] args) {

    PrintService pservice = PrintServiceLookup.lookupDefaultPrintService();
    String printText = "Hello World";
    try {
        SimpleDoc doc;
        doc = new SimpleDoc(printText.getBytes(), javax.print.DocFlavor.BYTE_ARRAY.AUTOSENSE, null);
        DocPrintJob job = pservice.createPrintJob();
        job.print(doc, new HashPrintRequestAttributeSet());
        System.out.println("Job sent to printer succesfully");

    } catch (Exception e) {
        e.printStackTrace();

    }

}
1 Answers

Sometime you need to send a Form Feed to force the printing.

InputStream ff = new ByteArrayInputStream("\f".getBytes());
Doc docff = new SimpleDoc(ff, flavor, null);
DocPrintJob jobff = service.createPrintJob();
pjw = new PrintJobWatcher(jobff);
Related