Mr CooL
Mr CooL

Reputation: 1579

Open PDF file on the fly from a Java application

Is there any way to have a code that opens a PDF file in Java application in a platform independant way? I mean using a batch file in Windows could do that. Is there any other way to have a platform independent code to open PDF files on the fly?

Upvotes: 47

Views: 119037

Answers (6)

Liam Maddren
Liam Maddren

Reputation: 1

3-rd party applications can not access src dir in your application, in case, when your app assemble in jar archive. You should place your file separately from src.

Of course, java find icons, because it's java API. You can access any resources in src folder through follow methods:

 URL url = getClass().getResource("/path/in/src");
 File file = new File(url.toURI());

I am currently using the below:

        if (Desktop.isDesktopSupported()) {
             try {
                    URL url = getClass().getResource("/pdf/XXXX.pdf");
                    File myFile = new File(url.toURI());
                    Desktop.getDesktop().open(myFile);
            } catch (IOException | URISyntaxException ex) {
                        // no application registered for PDFs
                }
            }

Upvotes: 0

Ed S
Ed S

Reputation: 249

Michael Meyer's solution didn't quite work for me. Specifically, a path with spaces fails with an IllegalArgumentException rather than an IOException.

Here's what works for me:

    if (Desktop.isDesktopSupported()) {
try {
File theUMFile = new File(usersManualPath);
 Desktop.getDesktop().open(theUMFile);
}
catch (FileNotFoundException fnf){
okDialog(msg_fnf);
theConcours.GetLogger().log(Level.SEVERE, null, fnf);
theConcours.GetLogger().info(msg_fnf);
}
catch (IllegalArgumentException fnf) {
 okDialog(msg_fnf);
            theConcours.GetLogger().log(Level.SEVERE, null, fnf);
            theConcours.GetLogger().info(msg_fnf);
        }
        catch (IOException ex) {
            okDialog(msg_cno);
            theConcours.GetLogger().log(Level.SEVERE, null, ex);
            theConcours.GetLogger().info(msg_cno);
        }
    } 

Upvotes: 2

Nimo ismail
Nimo ismail

Reputation: 1

Use this code to open a specific file:

String cmds[] = new String[] {"cmd", "/c", "C:\\Users\\PC\\Desktop\\EA01.pdf"};
try {
    Runtime.getRuntime().exec(cmds);
}

Upvotes: -3

Michael Myers
Michael Myers

Reputation: 191855

I'd try Desktop.open(File), which:

Launches the associated application to open the file.

So this code should do the trick:

if (Desktop.isDesktopSupported()) {
    try {
        File myFile = new File("/path/to/file.pdf");
        Desktop.getDesktop().open(myFile);
    } catch (IOException ex) {
        // no application registered for PDFs
    }
}

Upvotes: 101

amit
amit

Reputation: 84

Use this to open pdf file using java

File file = new File(filepath);
    if (file.toString().endsWith(".pdf")) 
        Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + file);
    else {
        Desktop desktop = Desktop.getDesktop();
        desktop.open(file);
}

This code is used to open your pdf and other files.

Upvotes: 2

mark stephens
mark stephens

Reputation: 449

You can use Runtime to execute and script and there are also several Java PDF viewers out there (ie Icepdf, JPedal, PDFRenderer).

Upvotes: 3

Related Questions