This is my simple print method. For some reason, I always got error
when I try to print
job.print(myDoc, aset);
Output:
Selected printer:LB1022
Print Error:java.io.IOException: No such file or directory
I tried printing it to the printer from the unix prompt with no
problem.
lp -d LB1022 some_file
and this works.
Any ideas??
public void print(String printer, String label) throws Exception {
InputStream textstream = null;
textstream = new ByteArrayInputStream(label.getBytes());
// Set the document type
DocFlavor myFormat =
DocFlavor.INPUT_STREAM.TEXT_PLAIN_US_ASCII;
// Create a Doc
Doc myDoc = new SimpleDoc(textstream, myFormat, null);
// Build a set of attributes
PrintRequestAttributeSet aset = new
HashPrintRequestAttributeSet();
// discover the printers that can print the format according
to the
// instructions in the attribute set
PrintService[] services =
PrintServiceLookup.lookupPrintServices(myFormat, aset);
int selectedPrinter=0;
// Check to see if the specify printer is a valid printer
for (int row=0; row<services.length; row++) {
if (services[row].getName().equals(printer)) {
selectedPrinter=row;
}
}
if (services[selectedPrinter] != null) {
System.out.println("Selected printer:" +
services[selectedPrinter].getName());
DocPrintJob job =
services[selectedPrinter].createPrintJob();
try {
job.print(myDoc, aset);
} catch (PrintException pe) {
System.out.println("Print Error:" + pe.getMessage());
}
}
}
Oliver Wong - 28 Mar 2006 18:37 GMT
> This is my simple print method. For some reason, I always got error
> when I try to print
[quoted text clipped - 3 lines]
> Selected printer:LB1022
> Print Error:java.io.IOException: No such file or directory
[...]
> DocPrintJob job =
> services[selectedPrinter].createPrintJob();
[quoted text clipped - 3 lines]
> System.out.println("Print Error:" + pe.getMessage());
> }
Try replacing the above with the following code, printing the output, and
giving it to us.
<code>
DocPrintJob job = services[selectedPrinter].createPrintJob();
try {
job.print(myDoc, aset);
} catch (PrintException pe) {
System.out.println(pe.getMessage());
System.out.println(pe.getClass().getName());
if (pe instanceof FlavorException) {
System.out.println("Flavor Exception");
FlavorException fe = (FlavorException)pe;
//I'm guessing this is not the problem.
System.out.pritnln("You guessed wrong.");
} else if (pe instanceof AttributeException) {
System.out.println("Attribute Exception");
AttributeException ae = (AttributeException)pe;
if (ae.getUnsupportedAttributes() != null) {
for (int i = 0; i < ae.getUnsupportedAttributes().size; i++) {
System.out.println("Unsupported attribute: " +
ae.getUnsupportedAttributes()[i]);
}
}
if (ae.getUnsupportedValues() != null) {
for (int i = 0; i < ae.getUnsupportedValues().size; i++) {
System.out.println("Unsupported values: " +
ae.getUnsupportedValues()[i].getName());
}
}
}
}
</code>
- Oliver
Alpha - 30 Mar 2006 04:23 GMT
Thanks, I'll try this when I get back to work tomorrow and post my
results. I'm suspecting that this may have something to do with the way
I setup the printer in the unix environment.