编译基本的java项目

时间:2016-11-17 11:33:48

标签: java jar manifest.mf

我想编译一个基本的java项目,https://sourceforge.net/projects/pdfformfiller2它的安装说明非常简短:

  

确保JAVA可以访问iText库,itext-xtra-5.x.0.jar和itextpdf-5.x.0.jar,   例如这些都放在" lib"当前文件夹的子文件夹。

     

https://sourceforge.net/projects/itext/files/iText/

获取最新内容      

编译PdfFormFiller.java

     

然后从命令行中给出命令(查看用法帮助):

     

java -jar pdfformfiller.jar

我以前从未编译过jar,而且我很难正确地尝试编译PdfFormFiller。在这里,我得到了:

wget -O pdfformfiller.zip https://sourceforge.net/projects/pdfformfiller2/files/latest/download
# author mentions 5.2.0, which is not available anymore, so we go for the latest 5.x:
wget http://kent.dl.sourceforge.net/project/itext/5.5.10/itext5-5.5.10.zip
unzip pdfformfiller.zip
unzip itext5-5.5.10.zip -d pdfformfiller/lib

cd pdfformfiller
javac -cp "lib/*" PdfFormFiller.java

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

哪个成功没有错误,但仍然无法运行:

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

我想我错过了一些微不足道的事情?

修改

完全自动化:

iText5=5.5.10

wget -O pdfformfiller.zip https://sourceforge.net/projects/pdfformfiller2/files/latest/download
wget http://kent.dl.sourceforge.net/project/itext/${iText5}/itext5-${iText5}.zip
unzip pdfformfiller.zip
unzip itext5-${iText5}.zip -d pdfformfiller/lib

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

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

修改2

这似乎是从CLI可靠地填写pdf表单的唯一方法:

# list fields in a file:
$ java -jar pdfformfiller.jar input.pdf -l
myfield

# prepare field data:
$ echo 'myfield αβγ' > fields

# specify font, fill the fields, flatten the form:
$ java -jar pdfformfiller.jar input.pdf -f fields -font Times_New_Roman.ttf -flatten output.pdf

像魅力一样!

1 个答案:

答案 0 :(得分:3)

以下是我为使其工作而采取的步骤。

  1. 首先,为了清楚起见,让我们为您编译的类创建一个专用文件夹。它不是强制性的,而只是良好开发实践的一个例子。我省略了创建文件夹,更改目录等步骤,因为它非常明显。所有命令都从项目的根目录

    运行
    javac -cp "lib/*" -d ./classes/ PdfFormFiller.java
    
  2. 修复错过的两件主要事情:

    a)所需lib文件夹和

    的参考

    b)包名:

    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. 装配罐子(请注意附加选项:-C在这里使用):

    jar -cvfm pdfformfiller.jar ./META-INF/MANIFEST.MF ./lib -C ./classes/ PdfFormFiller
    
  4. 这是执行生成的jar文件的最终输出:

    $ 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, http://www.itextpdf.com/