Java无法使用运行时打开pdf

发布于 2024-11-18 20:12:25 字数 2052 浏览 6 评论 0原文

我必须在单击 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应用程序运行时,我可以从menitem打开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);
                }
            }

        }
    });

时,所有 system.out.println() 的输出如下

run:

当从 netbeans 运行第二个代码F:/new/build/classes/newpkg/Documentation.pdf F:\new\build\classes\newpkg\Documentation.pdf 构建成功(总时间:5 秒)

I have to open a pdf on clicking a JMenuItem. I can open the pdf on click the menu item if i run my program from netbeans. But when i run from jar file it is not opening. I clean and build my project. But no change. Running when run from netbeans but not running from jar file.
Do i need to add some library.

My codes are as follows

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

Tried this as well but getting same thing.. i can open pdf from menuitem when i run from netbeans but not from jar application.

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

        }
    });

The output for all the system.out.println() is as follows when run from netbeans for this second code

run:

F:/new/build/classes/newpkg/Documentation.pdf
F:\new\build\classes\newpkg\Documentation.pdf
BUILD SUCCESSFUL (total time: 5 seconds)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不即不离 2024-11-25 20:12:25

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

..但仍然无法工作..

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

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

  1. 获取 < code>URL 到 PDF,就像现在所做的那样。
  2. 检查 URL 是否指向 Jar(它将包含“!”)。如果是的话..
    1. 将 PDF 从 Jar 提取到磁盘上的(临时)文件
  3. 显示(可能是临时的)文件

rundll32.exe can not deal with a resource that is now inside a Jar. Inspect the URL returned by getResource(String).

..but still not working..

The problem is that rundll32 was, for PDFs at least, only for File instances. The tools that consume (e.g. display) PDFs are generally not designed to accept command line args. representing an URL. If the URL should turn out to point to a File, the process can proceed. But once the PDF is in a Jar, it is just an entry in a Jar. Or to put that another way, it is not a File.

If that reasoning is correct, one way to get the PDF displayed in the default software is to:

  1. Get the URL to the PDF as done now.
  2. Check if the URL points to a Jar (it will contain a '!'). If it does..
    1. Extract the PDF from the Jar to a (temporary) File on disk.
  3. Display the (perhaps temporary) File.
鹤舞 2024-11-25 20:12:25

您可以使用 Java 6 和 DesktopAPI?

启动时您可以将文件导出或下载到磁盘吗?

Can you use Java 6 and the Desktop API?

and on startup can you export or download the file to disk?

心作怪 2024-11-25 20:12:25
 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
       }
 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
       }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文