0

I want to get the list of all the printers including network printers. It worked and got all the printers when the project was run on eclipse. But when i deployed the war in tomcat host manager and it loaded only the local printers. My code is this.

public static List<PrinterBean> ConnectedPrinters()
{
    List<PrinterBean> list=new ArrayList<PrinterBean>();
    PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
    for (PrintService printer : printServices){
        PrinterBean pb=new PrinterBean();
        pb.setPrinters(printer.getName());
        list.add(pb);
    }
    return list;
}
  • 1
    Are you running the tomcat on the same machine as the eclipse? Is the tomcat running as a service maybe using an account that is not able to see the printers? – DrHopfen Jun 22 '18 at 08:30
  • yes both are the same system. tomcat is running as service and deployed the war file. i can not understand the account? could you please make it clear. how can i change it? – Jitin Sasidharan Jun 22 '18 at 08:38
  • 1
    You need to either run the service with your user account or (which woulb be more clean) create a dedicated user to run the tomcat service, log in as that user and install/configure the printers for that user. Otherwise the tomcat will run under the system account which is unable to see the network printers. – DrHopfen Jun 22 '18 at 08:40
  • Logged in as administrator and the printers were installed and configured. The tomcat server starts automatically when system starts. Then how can i do it? – Jitin Sasidharan Jun 22 '18 at 08:45
  • 1
    Run services.exe -> right-click on Apache Tomcat -> Properties -> second tab (Log on) -> Choose account to run the tomcat (instead of local system account) in the lower part – DrHopfen Jun 22 '18 at 08:56
  • It worked!! Thank you so much @DrHopfen – Jitin Sasidharan Jun 22 '18 at 09:52
  • I'll write it as an answer, would be nice if you accept. – DrHopfen Jun 22 '18 at 13:14

1 Answers1

0

For the PrintServiceLookup to work on a Windows based tomcat installation it is necessary that the User running the tomcat service has the privileges to see the printer or rather has the printer configured.

This will usually not be the case when running under the local system account. If you need the network printers accesible by the tomcat you should create a dedicated user to run the tomcat and configure the network printers for him.

DrHopfen
  • 752
  • 3
  • 13