Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6374039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:26:21+00:00 2026-05-25T01:26:21+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T01:26:21+00:00Added an answer on May 25, 2026 at 1:26 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program that is been implemented in C++ which I now want
I have implemented tracing based on System.Diagnostics. I am also using a System.Diagnostics.TextWriterTraceListener, and
We have implemented a popup window as a modal dialog using the IE method:
I currently have a working, simple language implemented in Java using ANTLR. What I
All, I have Core Data implemented in my program to store customer information. Let's
I have created a program in visual c++, where i have implemented a web
I have Implemented a simple program which simulates $ls -l | wc -c command
I have implemented a simple chat program in Java . However when I run
How can I implement generics in this program so I do not have to
I have implemented what I thought was a pretty decent representation of MVC in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.