srikanth
srikanth

Reputation: 103

Java Printer Service throws ClassCastException while using PrinterURI Attribute

I have implemented a program, to print the document to the specific printer using IP address, printer name

Code:

URI myURI=null;
FileInputStream psStream=null;
try   {
       psStream = new FileInputStream( "sample.docx" );
   }
catch ( FileNotFoundException e )   {
    e.printStackTrace();
}
    DocFlavor psInFormat = DocFlavor.INPUT_STREAM.GIF;
    Doc myDoc = new SimpleDoc( psStream, psInFormat, null );
     PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    aset.add( new Copies(5) );
    try {             
        String host="192.255.301.147";
        String printer="HP LaserJet 5000 Series PCL6";
        String theUrl = "ipp://"+host+"/printers/"+printer;
        theUrl = URLEncoder.encode(theUrl, "UTF-8");
        myURI =  new URI(theUrl);       
              aset.add(new PrinterURI(myURI));  
    } catch (URISyntaxException e) {
        System.out.println("URI exception caught: "+e);         
    } catch (UnsupportedEncodingException e) {
             e.printStackTrace();
    }

  PrintService[] services = PrintServiceLookup.lookupPrintServices( psInFormat, aset );
   if ( services.length > 0 ) {
     DocPrintJob job = services[0].createPrintJob();          
     try {
             job.print( myDoc, aset );
     }
     catch ( PrintException e ){
     }
}

While running the program, got ClasscastException.

Exception in thread "Main Thread" java.lang.ClassCastException
    at javax.print.attribute.AttributeSetUtilities.verifyAttributeValue(AttributeSetUtilities.java:534)
    at javax.print.attribute.HashAttributeSet.add(HashAttributeSet.java:283)
    at com.src.print.TestPrint2.main(TestPrint2.java:64)

With out adding PrinterURI Attribute in the RequestAttributeSet (aset.add(new PrinterURI(myURI))), the Program is working fine. Its taking default printer configuration and printing the document.

Could you please help me out on this. how to use PrinterURI API?

Upvotes: 0

Views: 2153

Answers (2)

sonicli
sonicli

Reputation: 451

I have met the same error message "ClasscastException". I found it myself it is a careless mistake. Hope to help others.

PrintService[] services = PrintServiceLookup.lookupPrintServices( psInFormat, aset );

.lookupPrintServices is using PrintServiceAttributeSet

job.print( myDoc, aset );

.print is using PrintRequestAttributeSet

They are not same set of AttributeSet

Upvotes: 1

momo
momo

Reputation: 21353

From reading the stacktrace and related class documentation, this exception most likely occurs because HashPrintRequestAttributeSet (which extends PrintRequestAttributeset) enforces that all added type must conforms to PrintRequestAttribute.

If you see the documentation of PrinterURI, you would see that it does not implement that interface that cause the exception. Reading your code and question, I would guess you might want to use Destination instead of PrinterURI.

Upvotes: 0

Related Questions