ユーザーがアプリケーションで「HELP」をクリックしたときに PDF ファイルを開くことができるようにしたいと考えています。PDF ファイルは JAR とともに配置され、tmp ディレクトリに抽出され、awt.Desktop.getDesktop () によって選択されて開きます (Windows および Linux で使用できるようにするため)。
Eclipse からアプリを実行すると正常に動作し、PDF はエラーなしで開きます。JARにエクスポートして実行すると、「PDFドキュメントが破損しています」というエラーが表示されます.PDFドキュメントに手動で移動すると(私のubuntuマシン/tmp/546434564.pdf)、試してみると同じエラーが表示されますをクリックしてファイルを開きます。何が起こっているのか混乱しています。「破損した」PDF のファイル サイズは作業中の PDF と同じであるため、権限の問題ではないと思います。
私が使用しているコードは次のとおりです。
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();
}
}
});