Using Java, I need to print on network printer, which is not installed locally. I only know the printer name. All tutorials I've seen started with something like:
PrintService []services = PrinterJob.lookupPrintServices();
The problem is there can be no printers installed, so the services will be empty in that case. I need to set the printer name directly, not just enumerate through visible printers.
print(): print() method in Java is used to display a text on the console. This text is passed as the parameter to this method in the form of String. This method prints the text on the console and the cursor remains at the end of the text at the console.
Java System. out. println() is used to print an argument that is passed to it.
Java AWT Printing will not find a printer via a path if it is not registered to the Windows / Active Directory user that is running the printing application. You must register the printer path through Windows "Devices and Printers" as a printer for that user for it to be visible. Then, as that user, you must run lookupPrintServices
to see the list of printers available and retrieve the proper PrintService
by the exact name String
listed.
/**
* Retrieve the specified Print Service; will return null if not found.
* @return
*/
public static PrintService findPrintService(String printerName) {
PrintService service = null;
// Get array of all print services - sort order NOT GUARANTEED!
PrintService[] services = PrinterJob.lookupPrintServices();
// Retrieve specified print service from the array
for (int index = 0; service == null && index < services.length; index++) {
if (services[index].getName().equalsIgnoreCase(printerName)) {
service = services[index];
}
}
// Return the print service
return service;
}
/**
* Retrieve a PrinterJob instance set with the PrinterService using the printerName.
*
* @return
* @throws Exception IllegalStateException if expected printer is not found.
*/
public static PrinterJob findPrinterJob(String printerName) throws Exception {
// Retrieve the Printer Service
PrintService printService = PrintUtility.findPrintService(printerName);
// Validate the Printer Service
if (printService == null) {
throw new IllegalStateException("Unrecognized Printer Service \"" + printerName + '"');
}
// Obtain a Printer Job instance.
PrinterJob printerJob = PrinterJob.getPrinterJob();
// Set the Print Service.
printerJob.setPrintService(printService);
// Return Print Job
return printerJob;
}
/**
* Printer list does not necessarily refresh if you change the list of
* printers within the O/S; you can run this to refresh if necessary.
*/
public static void refreshSystemPrinterList() {
Class[] classes = PrintServiceLookup.class.getDeclaredClasses();
for (int i = 0; i < classes.length; i++) {
if ("javax.print.PrintServiceLookup$Services".equals(classes[i].getName())) {
sun.awt.AppContext.getAppContext().remove(classes[i]);
break;
}
}
}
In my case there was an authentication error, I could not find shared printer because i searched them with LocalUser account, using another account or changing printer grants I could find it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With