1

I'm creating a Java application using Netbeans. From the 'Help' Menu item, I'm required to open a PDF file. When I run the application via Netbeans, the document opens, but on opening via the jar file, it isn't opening. Is there anything that can be done?

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

        }
    });

The two outputs are as follows:

E:/new/build/classes/newpkg/Documentation.pdf
file:/E:/new/build/classes/newpkg/Documentation.pdf

Consider the above code snippet. On printing 'link',we can see that it is exactly same as the hard coded 'link3'. On using the hard coded 'link3' , the PDF file gets opened from jar application. But when we use link, though it is exactly same as 'link3', the PDF doesn't open.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • The first thing that can be done is posting some code. ;) – Jacob Jul 08 '11 at 13:20
  • possible duplicate of [Java unable to open pdf using Runtime.](http://stackoverflow.com/questions/6578300/java-unable-to-open-pdf-using-runtime) In fact, I'd say the OP either *copied* the same (broken) code, or *is* the OP of the duplicate thread. – Andrew Thompson Jul 08 '11 at 16:57
  • Uncompress the jar and get sure the pdf is in there, by default anything without .java doesn´t get to the jar in the normal compiling process. – Jaime Hablutzel Jul 08 '11 at 17:00
  • *"by default anything without .java doesn´t get to the jar"* I'm ..pretty sure that `.class` files make it into the Jar. – Andrew Thompson Jul 08 '11 at 17:05
  • @Andrew Thompson yes, it is about the same application. I guess the issue is pretty clear now,so do you have a solution? – R.S.Mukunth Jul 08 '11 at 18:04
  • I had a solution, which I gave to you, you marked correct, then changed your mind about - **3 days ago.** If you could not understand the solution then, or ask for clarification, why should I *waste my time* trying to explain it now? – Andrew Thompson Jul 08 '11 at 18:28
  • yes it did work, but the issue is that the hardcoded string works while the other doesnt, though both can be seen to be exactly the same.If this issue is addressed, the application will open the pdf irrespective of where the pdf is present. It will be helpful if you can address this particular issue for us once more here. Thanks. – R.S.Mukunth Jul 08 '11 at 18:33

2 Answers2

0

This is most likely related to the incorrect PDF resource loading. In the IDE you have the PDF file either as part of the project structure or with a directly specified relative path. When a packaged application is running it does not see the resource.

EDIT: Your code reveals the problem as I have described. The following method could be used to properly identify resource path.

public static URL getURL(final String pathAndFileName) {
    return Thread.currentThread().getContextClassLoader().getResource(pathAndFileName);
}

Pls refer to this question, which might provide additional information.

Community
  • 1
  • 1
01es
  • 5,362
  • 1
  • 31
  • 40
  • It was initially working in the packaged application . But after we added a few files to the project, it doesn't open anymore. So, the resource loading process must be okay, otherwise it wouldnt have opened initially from the application. – R.S.Mukunth Jul 08 '11 at 13:46
  • @R.S.Mukunth You can always check this by logging the path just before executing the command. – 01es Jul 08 '11 at 13:49
  • @R.S.Mukunth This is because the passed in path is not reachable. – 01es Jul 08 '11 at 13:59
  • @R.S.Mukunth I should clarify that the provided method is for determining URL of the resource in the jar of the application. – 01es Jul 08 '11 at 14:05
0

Try out this:

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);
            }
        }

    }
});
Gaurav
  • 55
  • 10