Java unable to open pdf using Runtime

2019-06-14 12:41发布

问题:

I have to open a pdf on clicking a JMenuItem. I can open the pdf on click the menu item if i run my program from netbeans. But when i run from jar file it is not opening. I clean and build my project. But no change. Running when run from netbeans but not running from jar file. Do i need to add some library.

My codes are as follows

m_aboutItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Runtime rt = Runtime.getRuntime();
           //System.out.println(Menubar1.getDefaultLocale());
                URL link2=Menubar1.class.getResource("/newpkg/Documentation.pdf");
                String link=link2.toString();
                link=link.substring(6);
                System.out.println(link);
                System.out.println(link2);
                String link3="F:/new/build/classes/newpkg/Documentation.pdf";
                try {
                Process proc = rt.exec("rundll32.exe url.dll,FileProtocolHandler " + link2);
            } catch (IOException ex) {
                Logger.getLogger(Menubar1.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    });

Tried this as well but getting same thing.. i can open pdf from menuitem when i run from netbeans but not from jar application.

m_aboutItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (Desktop.isDesktopSupported()) {
            Desktop desktop = Desktop.getDesktop();
            URL link2=Menubar1.class.getResource("/newpkg/Documentation.pdf");
                String link=link2.toString();
                link=link.substring(6);
                System.out.println(link); 
            File file=new File(link);
            System.out.println(file);
                try {
                    desktop.open(file);
                } catch (IOException ex) {
                    Logger.getLogger(Menubar1.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

        }
    });

The output for all the system.out.println() is as follows when run from netbeans for this second code

run:

F:/new/build/classes/newpkg/Documentation.pdf F:\new\build\classes\newpkg\Documentation.pdf BUILD SUCCESSFUL (total time: 5 seconds)

回答1:

rundll32.exe can not deal with a resource that is now inside a Jar. Inspect the URL returned by getResource(String).

..but still not working..

The problem is that rundll32 was, for PDFs at least, only for File instances. The tools that consume (e.g. display) PDFs are generally not designed to accept command line args. representing an URL. If the URL should turn out to point to a File, the process can proceed. But once the PDF is in a Jar, it is just an entry in a Jar. Or to put that another way, it is not a File.

If that reasoning is correct, one way to get the PDF displayed in the default software is to:

  1. Get the URL to the PDF as done now.
  2. Check if the URL points to a Jar (it will contain a '!'). If it does..
    1. Extract the PDF from the Jar to a (temporary) File on disk.
  3. Display the (perhaps temporary) File.


回答2:

Can you use Java 6 and the Desktop API?

and on startup can you export or download the file to disk?



回答3:

 try                                      //try statement
     {
         Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + "c:\\chart.pdf");   //open the file chart.pdf

     } catch (Exception e)                    //catch any exceptions here
       {
           System.out.println("Error" + e );  //print the error
       }