tags:

views:

356

answers:

1

Following is the code when I run on Linux,detects my printer and gives me print out.but when I run it on windows.It do not detect my printer.

printbutton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {

            DocFlavor docflavor = DocFlavor.INPUT_STREAM.TEXT_PLAIN_UTF_8;

            PrintRequestAttributeSet attr_set = new HashPrintRequestAttributeSet();

            attr_set.add(new Copies(1));

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

             if (service.length==0) {
                    JOptionPane.showMessageDialog(null, "No Printer Selected");
                }

            else if (service.length > 0) {
                System.out.println("Selected printer is " + service[0].getName());

                    DocPrintJob pj = service[0].createPrintJob();
                    {
                        PrintService ps = pj.getPrintService();
                        FileInputStream fis = null;
                        try {
                            File file = new File("c:\\NewFile.txt");
                            fis = new FileInputStream(file);
                            Doc doc = new SimpleDoc(fis, docflavor, null);
                            pj.print(doc, attr_set);
                        } catch (PrintException ex) {
                            Logger.getLogger(PrintButtonView.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (FileNotFoundException ex) {
                            Logger.getLogger(PrintButtonView.class.getName()).log(Level.SEVERE, null, ex);
                        } finally {
                            try {
                                fis.close();
                            } catch (IOException ex) {
                                Logger.getLogger(PrintButtonView.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        }
                    }
                }
            }
    });

}
A: 

Do you have a security manager installed, and the correct permissions ? From the doc:

A PrintServiceLookup implementor is recommended to check for the SecurityManager.checkPrintJobAccess() to deny access to untrusted code. Following this recommended policy means that untrusted code may not be able to locate any print services. Downloaded applets are the most common example of untrusted code.

This check is made on a per lookup service basis to allow flexibility in the policy to reflect the needs of different lookup services.

Services which are registered by registerService(PrintService) will not be included in lookup results if a security manager is installed and its checkPrintJobAccess() method denies access.

Brian Agnew