2

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.

3

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());        
        }
    }   
}
  • Oops I forgot one more important thing. CUPS Service in Ubuntu 16.04 is defaulting to on-demand mode and Java can't wake-up the CUPS service to query the printer. So I change the CUPS Service to always running by modifying /lib/systemd/system/cups.service from ExecStart=/usr/sbin/cupsd -l into ExecStart=/usr/sbin/cupsd -f – hadyatmo Mar 29 '17 at 3:07
  • Thank you very much for the detailed description. I will try what you suggest and hope it works for me too. – user630729 Mar 29 '17 at 7:06

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.