views:

71

answers:

3

I'm creating a Java app that creates 4 PDF files using iText. On the one that creates a PDF with an image in it, the .jar creates a 0 byte file and does not continue execution. However, when I Right Click >> Run As >> Java Application, it works just fine. To create the jar, I'm doing the following

  • Right click src
  • Export
  • Runnable JAR file
  • Extract required libraries into generated JAR
  • Finish

And the file "penguin.jpg" is under the src directory.

Here's my code

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileOutputStream;

public class ITextHelloWorld 
{
    public ITextHelloWorld() throws Exception
    {
        // Create the new document objects
        Document helloWorld = new Document();
        Document bigHello = new Document();
        Document linux = new Document();
        Document tables = new Document();


    /**********************************************************
                        start Big Hello.pdf
    This document is a huge document of text. Approximately 
    28 million characters, 24,391 pages, and 9.5 MB.
     **********************************************************/
    PdfWriter.getInstance(bigHello, new FileOutputStream("Big Hello.pdf"));
    bigHello.open();

    for (int i=0; i <1000000; i++)
    {
        bigHello.add(new Paragraph("Hello World. It's me again."));
    }

    bigHello.close();
    /**********************************************************
                        end Big Hello.pdf
     **********************************************************/

    /**********************************************************
                        start Tables.pdf
    This document will have tables in it
     **********************************************************/

    PdfWriter.getInstance(tables, new FileOutputStream("Tables.pdf"));
    tables.open();

    PdfPTable table=new PdfPTable(4);
    for (int i = 1; i<100; i++)
    {       
        table.addCell("This is cell #" + i + ".\n");
    }

    tables.add(table);
    tables.close();
    /**********************************************************
                        end Tables.pdf
     **********************************************************/

    /**********************************************************
                        start Linux.pdf
    This is a document that has text, graphics, and links.
     **********************************************************/
    PdfWriter.getInstance(linux, new FileOutputStream("Graphics and Text.pdf"));
    linux.open();       
    Image image = Image.getInstance("penguin.jpg");
    linux.add(image);

    linux.add(new Paragraph("Let's talk about Linux. \n\n" +
            "Linux (commonly pronounced /ˈlɪnəks/ LIN-əks in American English, also pronounced " +
            "/ˈlɪnʊks/ LIN-ooks in Europe and Canada) refers to the family of Unix-like computer " +
            "operating systems using the Linux kernel. Linux can be installed on a wide variety of " +
            "computer hardware, ranging from mobile phones, tablet computers and video game consoles, " +
            "to mainframes and supercomputers. Linux is predominantly known for its use " +
            "in servers; in 2009 it held a server market share ranging between 20–40%. Most desktop " +
            "computers run either Microsoft Windows or Mac OS X, with Linux having anywhere from a " +
            "low of an estimated 1–2% of the desktop market to a high of an estimated 4.8%. " +
            "However, desktop use of Linux has become increasingly popular in recent years, partly " +
            "owing to the popular Ubuntu, Fedora, Mint, and openSUSE distributions and the emergence" +
            " of netbooks and smartphones running an embedded Linux."));

    linux.close();
    /**********************************************************
                        end Linux.pdf
     **********************************************************/

    /**********************************************************
                        start Hello World.pdf
    This document is one line of text.
     **********************************************************/
    PdfWriter.getInstance(helloWorld, new FileOutputStream("Hello World.pdf"));
    helloWorld.open();
    helloWorld.add(new Paragraph("Hello World. It's me again."));
    helloWorld.close();
    /**********************************************************
                        end Hello World.pdf
     **********************************************************/

}

public static void main (String args[])
{
    try
    {
        new ITextHelloWorld();
    }

    catch (Exception e)
    {
        System.out.println(e);
    }
}

}

Thanks for any help!
Thomas

+4  A: 

Thomas, the problem is that when you create the jar, you 'mess up' the directory structure. You need to extract the image from the jar using the following method:

InputStream stream = this.getClass().getClassLoader()
                              .getResourceAsStream("/images/image.jpg");

You should edit the path to the image as necessary.

Your Image code will look something like this:

Image image = Image.getInstance(this.getClass().getResource("/penguin.jpg"));

Related question:

Java Swing Displaying Images From Within-a Jar

jjnguy
+1 - I remember having this issue when I first started working with executable JARs and coming out of Eclipse. It's not immediately obvious, so definitely check this out.
JasCav
Aha .. good observation Justin!
CoolBeans
@Cool, thanks. I've had this problem enough times to know the solution.
jjnguy
Thanks very much for your reply. Your explanation makes sense to me, but I'm afraid the code doesn't. I'm kinda new to Java. I tried it, but got "Type mismatch: cannot convert from BufferedImage to Image."
Thomas
@Thomas, hmmm, I thought `BufferedImage` was a subclass of image. Oops, at lease the answer above mine worked.
jjnguy
@Justin: He's using `com.itextpdf.text.Image` not `java.awt.Image`. It helps to check the imports. ;)
R. Bemrose
@RBem, ahh, that makes sense. Thanks for pointing that out!
jjnguy
@R.Bem, just in case the previous comment didn't trigger a notification.
jjnguy
Thanks folks, this dialog is helping me understand better.
Thomas
@Thomas, no problem. I didn't realize you weren't using Java's built-in `Image` class. That's why my code didn't work.
jjnguy
A: 

Without knowing the error you are getting, I will guess it's a CLASSPATH issue. When you run the jar file from command line you will either need to pass the classpath to point to the dependent jar files or the system classpath (environment variable) must point to all the jar files you need to run your application.

CoolBeans
+2  A: 

At a guess, the problem lies in this line:

Image image = Image.getInstance("penguin.jpg");

Since this is in the src directory, it's going to end up in the JAR file. However, you can't directly load files from a JAR file with just the filename.

However, Image.getInstance has an overload that takes a URL, which makes this rather easy:

Image image = Image.getInstance(this.getClass().getResource("/penguin.jpg"));

/ is the root of the src directory or jar file(s), not the filesystem root, in case you're wondering.

R. Bemrose
Thanks! this worked.
Thomas