Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 554
ashishjmeshram Avatar asked Oct 14 '15 03:10

ashishjmeshram


People also ask

How do I know if my printer is connected to my computer?

Select the Start button, then select Settings > Bluetooth & Devices > Printers & scanners . Make sure your printer is turned on and connected.

How do I see what Printers are connected to my network?

Find Printer on Network On Windows, type "cmd" in the search box on the Start Menu or task bar, then click the icon to load the Windows command prompt. Type "netstat" to list active connections, which may include your printer.

What is the command to find connectivity between computer and network printer?

The ping command is a Command Prompt command used to test the ability of the source computer to reach a specified network destination. The ping command is usually used as a simple way verify that a computer can communicate over the network with the printer.

How do I check my print server status?

Right-click on your printer to see a list of options. To view the print queue, select "See what's printing." To check general printer status, select "Properties," and to figure out if something is wrong with the printer select "Troubleshoot."


1 Answers

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.

like image 138
Westranger Avatar answered Oct 14 '22 13:10

Westranger