Java无法使用Runtime打开pdf

我必须在点击JMenuItem时打开一个pdf。 如果我从netbeans运行我的程序,我可以点击菜单项打开pdf。 但是当我从jar文件运行时它没有打开。 我清理并构建我的项目。 但没有变化。 从netbeans运行但不从jar文件运行时运行。 我需要添加一些库吗?

我的代码如下

m_aboutItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Runtime rt = Runtime.getRuntime(); //System.out.println(Menubar1.getDefaultLocale()); URL link2=Menubar1.class.getResource("/newpkg/Documentation.pdf"); String link=link2.toString(); link=link.substring(6); System.out.println(link); System.out.println(link2); String link3="F:/new/build/classes/newpkg/Documentation.pdf"; try { Process proc = rt.exec("rundll32.exe url.dll,FileProtocolHandler " + link2); } catch (IOException ex) { Logger.getLogger(Menubar1.class.getName()).log(Level.SEVERE, null, ex); } } }); 

试过这个但是得到了同样的东西..当我从netbeans运行而不是从jar应用程序运行时,我可以从menuitem打开pdf。

 m_aboutItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (Desktop.isDesktopSupported()) { Desktop desktop = Desktop.getDesktop(); URL link2=Menubar1.class.getResource("/newpkg/Documentation.pdf"); String link=link2.toString(); link=link.substring(6); System.out.println(link); File file=new File(link); System.out.println(file); try { desktop.open(file); } catch (IOException ex) { Logger.getLogger(Menubar1.class.getName()).log(Level.SEVERE, null, ex); } } } }); 

当从第二个代码的netbeans运行时,所有system.out.println()的输出如下所示

 run: 

F:/new/build/classes/newpkg/Documentation.pdf F:\ new \ build \ classes \ newpkg \ Documentation.pdf BUILD SUCCESSFUL(总时间:5秒)

rundll32.exe无法处理现在在Jar中的资源。 检查getResource(String)返回的URL。

..但仍然没有工作..

问题是,至少对于PDF来说, rundll32仅适用于File实例。 消耗(例如显示)PDF的工具通常不被设计为接受命令行参数。 代表一个URL 。 如果URL应该指向File ,则该过程可以继续。 但是一旦PDF在Jar中,它就只是Jar中的一个条目。 或者换句话说,它不是File

如果推理正确,在默认软件中显示PDF的一种方法是:

  1. 立即获取PDF的URL
  2. 检查URL指向Jar(它将包含’!’)。 如果它..
    1. 将PDF从Jar解压缩到磁盘上的(临时) File
  3. 显示(可能是临时的) File

你可以使用Java 6和Desktop API吗?

并在启动时可以导出或下载文件到磁盘?

  try //try statement { Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + "c:\\chart.pdf"); //open the file chart.pdf } catch (Exception e) //catch any exceptions here { System.out.println("Error" + e ); //print the error }