How to Check Printer Availability Using Java

Written by kevin walker
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Check Printer Availability Using Java
Check the status of the printer programmatically in Java. (The printer image by vin5 from Fotolia.com)

If you want to write a printer aware Java application, it is generally enough to create a PrintJob object using the javax.print library that comes with the SDK and let the operating system worry about printer availability. However, if you need a little more detail about what the default printer is capable of and whether it is online, you can use the PrinterAttributeSet to get more information about the printer.

Skill level:
Easy

Other People Are Reading

Instructions

  1. 1

    Create a PrinterStatusReport class. If you use an integrated development environment (IDE) like Netbeans or Eclipse, there will be an option in the "File" menu to automate this process. Otherwise, you can simply open any plain text editor like Notepad and save it with the name "PrinterStatusReport.java."

    Paste the following code inside it to define the class:

    public class PrinterStatusReport {

    // All other code will go here.

    }

  2. 2

    Add a main method to the class by pasting the following code within the brackets of the PrinterStatusReport:

    public static void main(String[] args) {

    }

  3. 3

    Paste the following code within the brackets of the main method:

    PrintService printer = PrintServiceLookup.lookupDefaultPrintService();
    
    AttributeSet att = printer.getAttributes();
    
    
    
    
    
    for (Attribute a : att.toArray()) {
    
        String attribute Name;
    
        String attribute Value;
    
        attribute Name = a.getName();
    
        attribute Value = att.get(a.getClass()).to String();
    
        System.out.println(attribute Name + " : " + attribute Value);
    
    
    
    }
    

    This grabs the default printer on the system and all of its attributes. Next, it goes through each attribute and prints out the attribute name and value, including its availability. With my own printer, the following is the output:

    printer-name : EPSON8EB46B

    colour-supported : supported

    printer-is-accepting-jobs : accepting-jobs

    queued-job-count : 1

    This indicates that my printer is an Epson, it supports colour, it is accepting new jobs, and currently has one job queued up for printing.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.