In this example we will show about how to open a PDF file in Java.
Source Code
package com.beginner.examples;
import java.io.File;
//Windows solution to view a PDF file
public class OpenPDF {
public static void main(String[] args) {
File file = new File("E:tmpTest.pdf");
try {
if (file.exists()) {
Process process = Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler E:tmpTest.pdf");
process.waitFor();
} else {
System.out.println("File is not exists");
}
System.out.println("Opening...");
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output:
Opening...
References
Imported packages in Java documentation: