从java启动pdf文件

从java启动pdf文件,java,netbeans,file-io,exception-handling,rundll32,Java,Netbeans,File Io,Exception Handling,Rundll32,我正在尝试从以下代码打开一个pdf文件 try { String currentDir = System.getProperty("user.dir"); currentDir = currentDir+"/Report.pdf"; System.out.println("Current dir using System:" +currentDir); if ((new File(currentDir)).

我正在尝试从以下代码打开一个pdf文件

try {  
         String currentDir = System.getProperty("user.dir");   
         currentDir = currentDir+"/Report.pdf";  
         System.out.println("Current dir using System:" +currentDir);  
         if ((new File(currentDir)).exists()) 
             {  
         Process p = Runtime  
         .getRuntime()
     .exec("rundll32 url.dll,FileProtocolHandler " +currentDir);  
     p.waitFor();  
     }   
         else 
             {    
        System.out.println("File is not exists");   
     }  
    System.out.println("Done");   
}
    catch (Exception ex) 
        {      
    ex.printStackTrace();    
    }  
print语句为我提供了文件的正确路径,即
当前目录使用
System:/Users/mshariff/NetBeansProjects/javaGUIbuilding/Report.pdf

但是程序给出了以下错误

java.io.IOException: Cannot run program "rundll32": error=2, No such file or directory

我使用的是
macosx
&
Netbeans

您手头的解决方案只能在Windows上运行(
rundll32
是一个Windows命令)

相反,你应该利用

例如

public static void main(String[] args) {
    try {
        Desktop desktop = Desktop.getDesktop();
        if (desktop.isSupported(Desktop.Action.OPEN)) {
            desktop.open(new File("Your.pdf"));
        } else {
            System.out.println("Open is not supported");
        }
    } catch (IOException exp) {
        exp.printStackTrace();
    }
}

你试过使用吗?如果您使用MacOS,您希望
rundll32
如何工作,因为这是一个Windows命令…?我可以用它打开文件,谢谢,但我现在遇到了一个新问题。当我运行程序时,文件只会第一次打开。当我关闭文件、更新并再次单击“打开”时。虽然文件已更新,但它不会再次打开;在全球范围内是静态的。所以每次我点击按钮。正在将report.pdf添加到字符串中。所以它先打开了。后来它的地址弄错了。