How to check java if the computer is connected to a network printer?

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

Is there a third-party API or tool for this?

I tried using PrintServiceLookup in java, but it does not give status if it is enabled or not.

Also, if this is not possible in java, is there any command that can be run on Windows that will give the printer status?

Then I can run this command in java and check.

+8
java network-printers
source share
6 answers

According to " How Network Printing Works, " it really depends on the type of printer and protocol that it supports. If you know the ip and port used by your printer, and if your printer supports SNMP (only for protocol selection), you can use the SNMP protocoll protocol to request your printer. There is a Java lib SNMP4j that can help you with this. I would suggest not using it if the printer, ip and port never (!) Change for your installation. This is due to the fact that you may encounter several problems.

  • How to detect an unknown printer?
  • How to open the port used by the printer?
  • How to detect the protocol used by the printer?

Assume that the above questions are not a problem and suggest that each printer will support SNMP. How to get information from this? Besides using the mentioned java lib, you can use snmpget on linux from the terminal. The syntax is as follows:

 snmpget -v1 -c public host-ip OID 

OID is an object identifier for each property of your printer, reaching from pagecount to toner cartridge information. If you do not add an OID, you will get the entire list of available OIDs. The bottom line is that although all OIDs are standardized, the use of OIDs differs from brand to brand and from printer model to printer model. For my HP, the following work:

 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" 

OID OID returns the printer status for my HP. But if I use the same OID on my Canon, I will 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, even SNMP is not applicable at all, not to mention other available protocols.

Given all this information, the easiest way in my view is to check whether it is possible to establish a connection to the printer on one of the printer's shared ports through 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 IP address of the printer.

+3
source share

If you know your printer IP address, you can use this code to verify it and verify if it accepts 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 modify this code a bit to use it in your case. Other than regular access, I see no better solution.

+2
source share

I don't think Java provides any portable API for status checking. Based on your requirement, you can check windows api

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

 call OpenPrinter2() then ClosePrinter() 

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

+1
source share

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

This might work in your case because this guy ( http://henryranch.net/software/jwmi-query-windows-wmi-from-java/ ) created jWMI that can query WMI systems in java. I am not sure how comprehensive it is, but you could assume that it could access Win32_Printer . It uses VBScript, which it runs in java via cmd.exe and gets the values ​​from stdout , so I have no idea about speed, but it also talks about using WMIC.exe, which may suit you.

From your ad it looks like your code could be as straightforward 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 may be rejected (e.g. 0x7 = power off, or 0x8 = off, etc.)

Queries also allow Select * from syntax so you can scroll through printers if you didn't have a name.

Win32_Printer has a property ( Network like Boolean) that allows you to check if the printer is local or network, and that is ( 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 when testing a network printer in VBScript.

These solutions are quite long in the tooth (I believe that MI is the latest version of WMI, for example), but if this jWMI library can work for you, this may be the answer.

+1
source share

There is a java api for native printing! view this java.awt.Desktop class
java2s.com eg

+1
source share

I used

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

All Articles