Open PDF file on the fly from a Java application

前端 未结 6 1879
逝去的感伤
逝去的感伤 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条回答
  •  萌比男神i
    2020-12-01 09:48

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

提交回复
热议问题