Print job gets to queue, but doesn't print

N

neriksmoen

I've searched high and low for a solution, but I have just had no luck
getting any output. I have a function that reads a text file and sends
the file to the printer.. however, it never reaches the printer,
rather, it just sits and hangs in the printers print queue. (and of
course its no simple task to delete that queued print job with the
simple "cancel" option in windows... have to open up cmd and type "net
stop spooler", then "net start spooler", then cancel the print job to
get it to delete). It acts almost like it doesn't close the print
stream when it reaches the end of the file, but I have no idea how to
close the stream once everything has been printed or spooled. Also a
side note, it doesn't lock up the java app when the printer is set to
"spool", but it does when it is set to accept an input stream...
anyhow, here's the code thats been causing this headache for a week...

public synchronized void PrintFile(String filename, String
selectedprinter)
{
selectedprinter = (selectedprinter == null)? defaultprinter :
selectedprinter;
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
PrintRequestAttributeSet attrib = new
HashPrintRequestAttributeSet();
PrintService svs[] =
PrintServiceLookup.lookupPrintServices(flavor, attrib);
int pindex = -1;
for(int a = 0; a < svs.length; a++)
{
String tmp = svs[a].toString();
if(tmp.compareTo(selectedprinter) == 0){ pindex = a; }
}
// can't print if selected printer isn't found...
if(pindex != -1)
{
PrintRequestAttributeSet aset = new
HashPrintRequestAttributeSet();

aset.add(MediaSizeName.ISO_A4);
aset.add(Sides.ONE_SIDED);
aset.add(new JobName(filename, null));
aset.add(new Copies(1));

DocPrintJob job = svs[pindex].createPrintJob();

try{
InputStream input = new BufferedInputStream(new
FileInputStream(filename));
PrintJobListener pjlistener = new PrintJobAdapter() {
public void
printDataTransferCompleted(PrintJobEvent e) {
System.out.println("Document sent to
printer.");
}
public void printJobCompleted(PrintJobEvent e) {
System.out.println("Document was successfully
printed.");
}
public void printJobCancelled(PrintJobEvent e) {
System.out.println("Document printing was
cancelled.");
}
public void printJobFailed(PrintJobEvent e){
System.out.println("Document failed to
print.");
}
public void printJobNoMoreEvents(PrintJobEvent e)
{
System.out.println("No more print events.");
}
public void
printJobRequiresAttention(PrintJobEvent e) {
System.out.println("Printer requires
attention.");
}
};
job.addPrintJobListener(pjlistener);
DocAttributeSet das = new HashDocAttributeSet();
das.add(Sides.ONE_SIDED);
Doc doc = new SimpleDoc(input, flavor, das);
job.print(doc, aset);

//input.close();
}
catch(Exception t){t.printStackTrace();}
}
else
{
// can't find selected printer
}
}

any help would be greatly appreciated. thanks in advance!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top