从JAR运行但从Eclipse运行时,从Java应用程序打开tmp PDF时出错 [英] Error opening tmp PDF from my Java application when run from JAR but not Eclipse
问题描述
当用户在我的应用程序中单击帮助"时,我希望能够打开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的文件大小与正常工作的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();
}
}
});
推荐答案
感谢帮助人员.通过导入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运行但从Eclipse运行时,从Java应用程序打开tmp PDF时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!