简体   繁体   中英

How to check in java if the PC is connected to the network printer?

Basically, I need to check the status of the n/w printer, if its on or not. Is there any way to do this in java?

Is there any third party API or tool for this?

I tried using PrintServiceLookup in java, but it does not give the status, if its on or not.

Also, if its not possible in java, is there any command that can be run in windows that will give the status of the printer?

Then I can run this command in java and check.

According to " How Network Printing Works " it really depends on the type of printer and the protocol which it supports. If you know the ip and the port used by you printer and if your printer supports SNMP (just to pick a protocol) you can use the SNMP protocoll to query your printer for information. There is the Java lib SNMP4j which can help you achive this. I would suggest to not use it unless the printer , the ip and the port will never (!) change for your setup. This is because you can run into several problems

  • How to discover an unknown printer ?
  • How to discover the port used by the printer ?
  • How to discover the protocoll used by the printer ?

Lets assume the questions above wouldn't be much of a problem and lets assume every printer would support SNMP. How to get informations out of it ? Besides using the mentioned java lib, you can use snmpget in linux from an terminal. The syntax is as follows:

snmpget -v1 -c public host-ip OID

The OID is an object identifier for every property of you printer reaching from pagecount to toner-cardridge information. If you don't add an OID you'll get the whole list of available OID's. The crux of the matter is although all OID's are standardized the usage of the OID's differ from brand to brand and from printer-model to printer-model. For my HP the following works:

snmpget -v1 -c public 192.168.1.10 iso.3.6.1.2.1.43.17.6.1.5.1.2

and returns

iso.3.6.1.2.1.43.17.6.1.5.1.2 = STRING: "Ready"

the use OID returns the status of the printer for my HP. But if I use the same OID on my Canon I'll get

Error in packet
Reason: (noSuchName) There is no such variable name in this MIB.
Failed object: iso.3.6.1.2.1.43.17.6.1.5.1.2

Therefore it is not even SNMP generically applicable, not mentioning the other protocols which are available.

Considering all these information, the easiest way in my opinition is just check if you can establish the connection to the printer on one of the common printer ports via this code

boolean available = false;
try {
    String serverAddress = "192.168.1.10";
    Socket s = new Socket(serverAddress, 9100);
    s.close();
    available = true;
} catch (IOException e) {
    available = false;
}
System.out.println("printer available: " + available);

Of course this only works if you already know the printer ip.

If you know your printer IP you could use this code to ping it and check if it's accepting jobs.

import java.io.IOException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.attribute.Attribute;
import javax.print.attribute.AttributeSet;

public class PrinterStatus {

    public static void main(String[] args) {

    PrintService printer = PrintServiceLookup.lookupDefaultPrintService();
    AttributeSet att = printer.getAttributes();

    String ip = "0.0.0.0"; // IP Address of your printer
    boolean check = false;

    for (Attribute a : att.toArray()) {
        if (att.get(a.getClass()).toString().equalsIgnoreCase("accepting-jobs")){
            check = true;
        }
    }

    if (check && runSystemCommand(ip)) System.out.println("Printer ready!");
}

public static boolean runSystemCommand(String ip) {

    ProcessBuilder pb = new ProcessBuilder("ping", "-c 1", ip);
    Process p;
    try {
        p = pb.start();
        return p.waitFor() == 0 ? true : false;
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return false;
  }
}

Of course you need to change this code a little to use it in your case. Except native accessing, I don't see better solution.

I don't think Java provide any portable API for you to check the status. Based on your requirement, you may want to check the windows api

https://msdn.microsoft.com/en-us/library/windows/desktop/dd162752(v=vs.85).aspx

call OpenPrinter2() then ClosePrinter() 

with a little of JNI codes this will be trivial, you may also want to consider https://github.com/java-native-access/jna to make the native accessing part of your code easier

I can't think of an easy way to do this in java (no doubt someone will come up with a single line of code though!). I'm more familiar with C# and, to answer your Windows question, I'd use Microsoft's WMI, specifically Win32_Printer ( https://msdn.microsoft.com/en-us/library/aa394363(v=vs.85).aspx ). An old CodeProject site shows how to test for an offline printer in C# using Win32_Printer ( http://www.codeproject.com/Articles/6069/How-to-Check-if-your-Printer-is-Connected-using-C ).

It might work in your case because this guy ( http://henryranch.net/software/jwmi-query-windows-wmi-from-java/ ) has produced jWMI which can query the WMI systems in java. I'm not sure how exhaustive it is, but you'd imagine it could access Win32_Printer . It uses VBScript which it executes within java via cmd.exe and obtains values from stdout so I've no idea about speed, but he also talks about using WMIC.exe which might suit you better.

From his blurb, it looks as if your code could be as straight forward as:

String status = getWMIValue("Select [printer name] from Win32_Printer", "PrinterStatus");

where 7 (0x7) is offline.

or

String status = getWMIValue("Select [printer name] from Win32_Printer", "Availability");

where various states can be discerened (such as 0x7 = power off, or 0x8 = off line, etc.)

The queries also allow a "Select * from" syntax so you could loop through the printers if you didn't have a name.

Win32_Printer has a property ( Network as Boolean) that allows you to check whether the printer is local or network, and this ( http://blogs.technet.com/b/heyscriptingguy/archive/2006/08/14/how-can-i-list-all-the-printers-on-a-remote-computer.aspx ) is an old, but interesting, read on testing for a network printer in VBScript.

These solutions are pretty long in the tooth (I believe MI is the most recent version of WMI, for example), but if that jWMI library can work for you, it might be an answer.

there is java api to native printing facility !! check out this class java.awt.Desktop
java2s.com eg

i have used

String WorkOffline = getWMIValue("Select * 
from Win32_Printer 
where Name='printer_name'", "WorkOffline");
returns True/False

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM