Fahim Parkar
Fahim Parkar

Reputation: 31637

PDF file not opening

I have Java code to open the pdf file in Java and it is running perfectly.

Code is

    try {
        File pdfFile = new File("/Users/alkandari/Desktop/SMART/Fahim/test.pdf");
        if (pdfFile.exists()) {
            if (Desktop.isDesktopSupported()) {
                Desktop.getDesktop().open(pdfFile);
            } else {
                System.out.println("Awt Desktop is not supported!");
            }
        } else {
            System.out.println("File is not exists!");
        }
        System.out.println("Done");
    } catch (Exception ex) {
        ex.printStackTrace();
    }

However when I put this in some method say showMyPDFFile() and try to call that on commandLink, pdf doesn't get open. It says Awt Desktop is not supported!.

 <h:commandLink value="View PDF/ DOC"
 action="#{PersonalInformationDataBean.showMyPDFFile()}" />

Any idea why pdf is not getting opened?

Upvotes: 0

Views: 517

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500225

So just to check, you're writing a web application, right?

The Desktop class refers to the local desktop - so it's for local GUI client applications. Even if it were supported, you'd be opening the PDF on the server's desktop - not the client's desktop, which I assume is what you're after.

It sounds like you should basically just be linking straight to the PDF file (as URL). You'll need to make the PDF available via your web server, of course.

Upvotes: 2

Related Questions