从JAR运行时从我的Java应用程序打开tmp PDF时出错,但不是elipse [英] Error opening tmp PDF from my Java application when run from JAR but not elipse

查看:89
本文介绍了从JAR运行时从我的Java应用程序打开tmp PDF时出错,但不是elipse的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在用户点击我的应用程序中的帮助时打开PDF文件。 PDF文件位于JAR中,解压缩到tmp目录,然后通过awt.Desktop.getDesktop()选择打开(允许使用Windows和Linux)。

I want to be able to open a PDF file when the user clicks on 'HELP' in my application. The PDF file is located with the JAR, extracted to a tmp directory and then selected to open by awt.Desktop.getDesktop () (to allow for windows and linux use).

当我从Eclipse运行应用程序然后它工作正常,PDF打开没有错误。当我导出到JAR然后运行然后我得到一个错误,说明'PDF文档已损坏',如果我手动导航到PDF文档(在我的ubuntu机器上/tmp/546434564.pdf),那么当我尝试时我得到相同的错误打开文件。我很困惑发生了什么事。 损坏的PDF的文件大小与工作文件大小相同,所以我认为这不是许可问题。

When I run the app from Eclipse then it works fine, the PDF opens with no errors. When I export to JAR and run then I get an error stating the 'PDF document is damaged', if I navigate manually to the PDF document (on my ubuntu machine /tmp/546434564.pdf) then I get the same error when I try to open the file. I am confused what is going on. The file size of the 'damaged' PDF is the same as the working one, so I dont think it is a permission issue.

我使用的代码是:

public Main() throws FontFormatException, IOException {

        //lets load the font
           Font font = Font.createFont(Font.TRUETYPE_FONT, this.getClass().getResourceAsStream("/Coalition_v2.ttf")).deriveFont(Font.PLAIN, 14); 
           System.out.println(font);

        //lets write the tmp file for help to the machine now
           try {
                java.io.InputStream iss = getClass().getResourceAsStream("/nullpdf.pdf"); //update the filename here when the help guide is written
                byte[] data = new byte[iss.available()];
                iss.read(data);
                iss.close();
                String tempFile = "file";
                File temp = File.createTempFile(tempFile, ".pdf");
                FileOutputStream fos = new FileOutputStream(temp);
                fos.write(data);
                fos.flush();
                fos.close();
            tmphelpfile = temp.getAbsolutePath();
            System.out.println(tmphelpfile);
            } catch (IOException ex) {
                ex.printStackTrace();
                System.out.println("TEMP FILE NOT CREATED - ERROR in tmp file writing");
            }

然后拨打pdf:

JMenu mnHelpGuide = new JMenu("Help Guide");
        mnHelpGuide.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
            //  Help();
                Desktop d = java.awt.Desktop.getDesktop ();  

                try {
                    System.out.println(tmphelpfile);
                    d.open (new java.io.File (String.valueOf(tmphelpfile)));
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    System.out.println("Couldnt open your file - error on HELP Guide");
                    e1.printStackTrace();
                } 
            }
            });

感谢您提供任何帮助;

Thank you for any help you can offer;

Andy

推荐答案

感谢帮助人员。通过导入ApachecommonsIO jar然后将代码修改为:

Thanks for the help guys. The problem was fixed by importing the ApachecommonsIO jar and then modifying the code to this:

 try {
                java.io.InputStream iss = getClass().getResourceAsStream("/nullpdf.pdf"); //update the filename here when the help guide is written
               // byte[] data = new byte[iss.available()];
                byte[] data = IOUtils.toByteArray(iss);
                iss.read(data);
                iss.close();
                String tempFile = "file";
                File temp = File.createTempFile(tempFile, ".pdf");
                FileOutputStream fos = new FileOutputStream(temp);
                fos.write(data);
                fos.flush();
                fos.close();
            tmphelpfile = temp.getAbsolutePath();
            System.out.println(tmphelpfile);
            } catch (IOException ex) {
                ex.printStackTrace();
                System.out.println("TEMP FILE NOT CREATED - ERROR in tmp file writing");
            }

这篇关于从JAR运行时从我的Java应用程序打开tmp PDF时出错,但不是elipse的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆