0
我在SO上遇到了這個問題。
Java application runs properly in Eclipse, but not as .jar
我沒有在我的code.I創建以下列方式運行的JAR文件中的任何圖片,
如何使用pdf生成代碼導出eclipse項目(itext)
- 右鍵單擊項目,
- 單擊導出,
- 選擇「 Runnable的JAR文件」,
- 提取所需的庫到生成JAR
當我在桌面上運行.jar文件時,正在創建PDF文件。 但它顯示了以下錯誤
ADOBE READER無法打開「結果,itext.pdf」,因爲它要麼是 不支持的文件類型,或者因爲文件已損壞
我的代碼:
try {
PdfWriter w = new PdfWriter("Result-itext.pdf");
PdfDocument d = new PdfDocument(w);
Document doc = new Document(d);
/** Added **/
Image img = new Image(ImageDataFactory.create(logo));
img.setHorizontalAlignment(HorizontalAlignment.CENTER);
doc.add(img);
/** Added **/
doc.add(new Paragraph("Test Name : Hello World").setTextAlignment(TextAlignment.CENTER));
doc.add(new Paragraph("Maximum Marks : 20").setTextAlignment(TextAlignment.CENTER));
doc.add(new Paragraph("RESULTS").setBold().setTextAlignment(TextAlignment.CENTER));
PdfFont font = PdfFontFactory.createFont(FontConstants.HELVETICA_OBLIQUE);
Table t = new Table(3);
t.setWidthPercent(70);
t.setHorizontalAlignment(HorizontalAlignment.CENTER);
t.setFont(font);
Cell cell = new Cell().add("User-ID").setTextAlignment(TextAlignment.CENTER).setFont(font);
t.addCell(cell);
cell = new Cell().add("User-Name").setTextAlignment(TextAlignment.CENTER).setFont(font);
t.addCell(cell);
cell = new Cell().add("Marks").setTextAlignment(TextAlignment.CENTER).setFont(font);
t.addCell(cell);
PdfFont font1 = PdfFontFactory.createFont(FontConstants.TIMES_ROMAN);
t.setFont(font1);
ArrayList<String> a = new ArrayList<String>();
for(int i=0;i<3;i++){
a.add(String.valueOf(i));a.add("jack");a.add(String.valueOf(i+10));
}
for(int i=0;i<9;i++){
cell = new Cell().add(a.get(i)).setTextAlignment(TextAlignment.CENTER);
t.addCell(cell);
}
doc.add(t);
doc.close();
JOptionPane.showMessageDialog(null, "Created file");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我糾正了一些錯誤。然後我添加了圖像顯示代碼。它現在顯示java.io.FileNotFoundException C:\ Users \ Pradeep \ Desktop \ resources \ logo.jpg(系統找不到路徑指定)' – Pradeep
嘗試檢查logo.jpg是否存在。如果存在 –
https://docs.oracle.com/javase/7/docs/api/java/io/FileNotFoundException.html。 JavaDoc說,甚至當試圖打開一個只讀文件來編寫時,拋出異常 –