Adobe Adobe - 1 year ago 163
Java Question

Compiling basic java project

I want to compile a basic java project, its installation instructions are quite short:

Make sure that iText library, itext-xtra-5.x.0.jar and itextpdf-5.x.0.jar, are accessible to JAVA,
e.g. these are placed in the "lib" subfolder of the current folder.

Get latest ones from:


Then from the command line you give command (to see usage help):

java -jar pdfformfiller.jar

I never compiled jars before, and I'm having hard time trying to compile PdfFormFiller correctly. Here's where I've get:

wget -O
# author mentions 5.2.0, which is not available anymore, so we go for the latest 5.x:
unzip -d pdfformfiller/lib

cd pdfformfiller
javac -cp "lib/*"

mkdir META-INF
echo -e 'Manifest-Version: 1.0\nClass-Path: pdfformfiller.jar\nMain-Class: PdfFormFiller' > META-INF/MANIFEST.MF
jar -cvfm pdfformfiller.jar META-INF/MANIFEST.MF lib PdfFormFiller.class

Which succeeds without an error, but still doesn't run:

$ java -jar pdfformfiller.jar
Error: Could not find or load main class PdfFormFiller

I guess I'm missing something trivial?


Complete automation:


wget -O
unzip itext5-${iText5}.zip -d pdfformfiller/lib

cd pdfformfiller
mkdir classes
javac -cp "lib/*" -d ./classes/

mkdir META-INF
echo 'Manifest-Version: 1.0' > META-INF/MANIFEST.MF
echo "Class-Path: ./lib/itextpdf-${iText5}.jar ./lib/itext-xtra-${iText5}.jar ./lib/itext-pdfa-${iText5}.jar" >> META-INF/MANIFEST.MF
echo 'Main-Class: PdfFormFiller.PdfFormFiller' >> META-INF/MANIFEST.MF

jar -cvfm pdfformfiller.jar ./META-INF/MANIFEST.MF ./lib -C ./classes/ PdfFormFiller

Answer Source

Here are the steps I followed to get it working.

  1. First of all, just for the sake of clarity, let's create a dedicated folder for your compiled classes. It's not mandatory, but just an example of good development practice. I'm omitting the steps of creating folders, changing dirs etc. because it's quite obvious. All commands are run from the project's root directory

    javac -cp "lib/*" -d ./classes/
  2. Fixing the two main things that were missed:

    a) the reference for required lib folder and

    b) package name:

    echo -e 'Manifest-Version: 1.0\nClass-Path: ./lib/itextpdf-5.5.4.jar ./lib/itext-xtra-5.5.4.jar ./lib/itext-pdfa-5.5.4.jar\nMain-Class: PdfFormFiller.PdfFormFiller' > META-INF/MANIFEST.MF
  3. Assembling jar (please note that additional option: -C is being used here):

    jar -cvfm pdfformfiller.jar ./META-INF/MANIFEST.MF ./lib -C ./classes/ PdfFormFiller
  4. This is the final output from executing the resulting jar file:

    $ java -jar pdfformfiller.jar
    USAGE: pdfformfiller document.pdf [ -l ] [ -v ] [ -f fields_filename ] [ -font font_file ] [ -flatten] [ output.pdf ]
        document.pdf - name of source pdf file (required).
        -l - only list available fields in document.pdf.
        -v - verbose. Use to debug the fields_filename file.
        -f fields_filename - name of file with the list of fields values to apply to document.pdf.
                             if ommited, stdin is used.
        -font font_file - font to use. Needed UTF-8 support, e.g. cyrillic and non-latin alphabets.
        -flatten - Flatten pdf forms (convert them to text disabling editing in PDF Reader).
        output.pdf - name of output file. If omitted, the output if sent to stdout.
    fields_filename file can be in UTF-8 as is of the following format:
        On each line, one entry consists of 'field name' followed by value of that field without any quotes.
        Any number of whitespaces allowed before 'field name', and one space separates 'field name' and its value.
        In value, newline characters should be encoded as "\n",
        'U+2029 utf-8 E280A9 : PARAGRAPH SEPARATOR PS' should be encoded as "\p",
        and '\' characters should be escaped as "\\".
        For checkboxes, values are 'Yes'/'Off'.
        Based on the Belgian iText library v. 5.2.0,
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download