Velvet Star Monitor

Standout celebrity highlights with iconic style.

updates

Java printing with CUPS : printers not found

Writer Matthew Barrera

I'm trying to setup a Tomcat server (8.5.5.0, Java 1.8.0_101) on Ubuntu (16.04), and print to shared printers on a Windows print server.

I have successfully configured several printers with CUPS (2.1.3) and SAMBA (4.3.11). Printing from command line works fine.

In my Java application, when I try to lookup printers like this :

PrintService[] ps = PrintServiceLookup.lookupPrintServices(null, null);

It gives me an empty array. But if I set a printer as default ("DefaultPrinter" in /etc/cups/printers.conf), then the array contains only this printer.

So currently I can print only to the default printer, but I want to be able to print to any printer.

Thanks for your help.

1 Answer

I had the same problem and just found the solution. I don't think this is the best way to solve the problem, but at least it's working for me.

I first found the problem when trying to set up CUPS service on a headless Ubuntu 16.04 server. On my desktop Ubuntu 16.04 the lookup printer is working even without setting a default printer. After further investigation I found that the problem occured after I changed the CUPS configuration to enable remote administration.

Original configuration from cupsd.conf:

# Only listen for connections from the local machine
Listen localhost:631
Listen /var/run/cups.sock

Modifed configuration to enable remote administration (not working):

# Only listen for connections from the local machine
Listen *:631
Listen /var/run/cups.sock

It seems that Java needs to connect to the CUPS service using localhost, so to enable remote administration and also provide localhost connection for Java:

# Only listen for connections from the local machine
Listen localhost:631
Listen /var/run/cups.sock
Listen 192.168.0.100:631

I still don't understand why using Listen *:631 can't provide localhost connection for Java so my workaround is to add configuration to listen for my server ip address.

Here's my Java code that I used to query the CUPS printer

public class TestPrinter { public static void main(String[] args) { PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null); System.out.println("Number of print services: " + printServices.length); System.out.println("Available printer: "); PrintService mPrintService = null; for (PrintService printer : printServices) { System.out.println(printer.getName()); } PrintService defaultPrinter = PrintServiceLookup.lookupDefaultPrintService(); if (defaultPrinter != null) { System.out.println("Default printer: " + defaultPrinter.getName()); } }
}
2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy