Open PDF file on the fly from a Java application

前端 未结 6 1899
逝去的感伤
逝去的感伤 2020-12-01 09:06

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 oth

6条回答
  •  没有蜡笔的小新
    2020-12-01 09:53

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

提交回复
热议问题