0

I came across this question on SO.
Java application runs properly in Eclipse, but not as .jar
I don't have any images in my code.I created a runnable jar file in the following way,

  • Right click on project,
  • Click Export,
  • select "Runnable JAR File",
  • Extract required libraries into generated JAR

When I run the .jar file on my desktop, the PDF file is being created. But it shows the following error

Adobe Reader could not open 'Result-itext.pdf' because it is either not a supported file type or because the file has been damaged

My Code:

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();
        }
Community
  • 1
  • 1
Pradeep
  • 1,193
  • 6
  • 27
  • 44

1 Answers1

0

Couple pointers to help with your problem

  1. Compare the two PDF file size, first one generated from eclipse, second generated from the jar file. Is there a size difference, if there is then it means that the generated jar is missing something that the eclipse project has.
  2. Are you running the generated jar by double-clicking it? If 'yes' then even if there is any error thrown by any program in jar file, it won't appear as the window closes immediately (assuming that this is no Swing/AWT GUI application). So I suggest to run it from command prompt like: java -jar xyz.jar

Hopefully these two should resolve your issue.

S R Chaitanya
  • 738
  • 7
  • 11
  • I've correct some of my mistakes.Then I added the Image display code.It now shows `java.io.FileNotFoundException C:\Users\Pradeep\Desktop\resources\logo.jpg (The system cannot find the path specified) ` – Pradeep Dec 29 '16 at 13:06
  • Try to check if the logo.jpg exists or not. If exists – S R Chaitanya Dec 29 '16 at 13:13
  • https://docs.oracle.com/javase/7/docs/api/java/io/FileNotFoundException.html. JavaDoc says, the exception is thrown even when an attempt is made to open a read-only file for writing – S R Chaitanya Dec 29 '16 at 13:15
  • Yes.It exists in the `resources` folder.It works properly in eclipse.But when exported to a jar file,it's causing excpetions. – Pradeep Dec 29 '16 at 13:16
  • Ideally, absolute path must work, unless there is an issue with accessing the resource from the given location. You can try to create a new folder in C drive, add both image and jar file (the program must be updated to point to latest path) and run the program. – S R Chaitanya Dec 29 '16 at 13:23