0

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)

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Gaurav
  • 55
  • 10

3 Answers3

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.
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Thanks ! Yes this works,but we need to distribute this software, therefore we cannot hardcode the file path. I tried the following but still not working String link=link2.toString(); Sopln(link) was: file:/F:/new/classes/build/newpkg/Documentation.pdf So i extracted the path only by link=link.substring(6); Sopnl(link) now is: F:/new/classes/build/newpkg/Documentation.pdf if i write getResource(link) again same thing is happening. If i directly write link="F:/new/classes/build/newpkg/Documentation.pdf";. It works as you said in both netbeans and jar. Is there any way to make path un-hardcoded – Gaurav Jul 05 '11 at 11:52
  • Thanks a lot for replying so fast. And i tried to print the variable link2. The output was: file:/F:/new/build/classes/newpkg/Documentation.pdf only Sry i interchanged build and classes in the above comment. Okay... can you give few more details how to do that.. i mean any url or tutorial i can go through for that... – Gaurav Jul 05 '11 at 13:32
  • *"The output was: `file:/F:/new/build/classes/newpkg/Documentation.pdf` only"* Are you sure that is the output when you 'run from Jar file' - I.E. when it *fails?* I am surprised you pasted that URL, since it contains no **`!`** and should work (according to my best theories). – Andrew Thompson Jul 05 '11 at 13:48
  • I am surprised as well. Look at the code... i edited it.. Printing both link2 and link Output in netbeans window is run: F:/new/build/classes/newpkg/Documentation.pdf file:/F:/new/build/classes/newpkg/Documentation.pdf BUILD SUCCESSFUL (total time: 8 seconds) When i uses link3 in exec command... everything is working..While link3 is same as link. is just to make symbolic representation of change of line – Gaurav Jul 05 '11 at 14:01
  • Two days before it was working with link2 only. I changed few codes but havent changed anything in that menu... it stops working now.. i dont have any idea why.. i thought... may be i need to bind few libraries – Gaurav Jul 05 '11 at 14:04
  • And i just copied pasted the output "Sopln's" from the output window of the netbeans..so this is it.. – Gaurav Jul 05 '11 at 14:54
  • *"from the output window of the netbeans.."* Will you leave your *bl00dy* IDE out of it for a minute? I am not interested in the output where it works in the IDE, **but the output where it breaks.** – Andrew Thompson Jul 05 '11 at 15:00
1

Can you use Java 6 and the Desktop API?

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

l_39217_l
  • 2,090
  • 1
  • 13
  • 15
  • Not for a resource in a Jar file. `Desktop.open(File)/.edit(File)` methods each expect a `File`. Resources in Jars are only available by `URL`. (And `Desktop.browse(URI)` passes the URI to a browser, which is equally unlikely to be able to load resources from archives.) – Andrew Thompson Jul 05 '11 at 14:28
  • Tried this too.. This is also working but from netbeans not from jar.. i edited and added the codes for this as all... here also i tried Sopln() for assistance.. all are written above.. I dont know what to do now.. and yes i clean and build my project before opening the jar file and open the correct jar file only.. – Gaurav Jul 05 '11 at 14:50
0
 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
       }
Rupok
  • 2,062
  • 16
  • 16
  • 1
    *"it works for me"* Luck must be on your side, perhaps you should buy a lottery ticket. Please review [When Runtime.exec() won't](http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html) & incorporate the advice into your (code &) future answers. – Andrew Thompson Jul 05 '11 at 12:37
  • Thanks it works but path is quite hard-coded.. i dont want to hard core.. as the software is for distribution... please check my question once again as i edited.. n reply.. – Gaurav Jul 05 '11 at 14:52