代码片段
public class AnyPlatformAppPDF {
public static void main(String[] args) {
try {
File pdfFile = new File("c:\\Users\\ADMIN\\Desktop\\css\\Praveen_Profile.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();
}
}
public static void openWebpage(java.net.URI uri) {
Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
try {
desktop.browse(uri);
} catch (Exception e) {
e.printStackTrace();
}
}
我尝试使用此代码在浏览器中打开pdf文件,但它不会打开pdf文件。我正在使用Java这样做。 我该如何解决这个问题?
答案 0 :(得分:1)
使用流程构建器可以解决您的问题;
//视窗
ProcessBuilder processBuilder = new ProcessBuilder("cmd.exe", "/C", "explorer c:\\Users\\ADMIN\\Desktop\\css\\Praveen_Profile.pdf");
// Linux的
ProcessBuilder processBuilder = new ProcessBuilder("/bin/bash", "-c", "sensible-browser c:\\Users\\ADMIN\\Desktop\\css\\Praveen_Profile.pdf");
//
processBuilder.start();