3

ダウンロードitext-1.3して、のlibフォルダーに配置しましjdk 1.6.0た。また、lib フォルダーをシステム変数の CLASSPATH として追加しました。

しかし、プログラムを実行するとエラーが発生します:

パッケージ com.itextpdf.text が存在しません。

他のすべてのパッケージについても同様です。私が犯した間違いは何ですか?

import java.io.FileOutputStream;
import java.io.IOException;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

/**
 * First iText example: Hello World.
 */
public class HelloWorld {

    /** Path to the resulting PDF file. */
    public static final String RESULT
        = "E:/hello.pdf";

    /**
     * Creates a PDF file: hello.pdf
     * @param    args    no arguments needed
     */
    public static void main(String[] args)
        throws DocumentException, IOException {
        new HelloWorld().createPdf(RESULT);
    }

    /**
     * Creates a PDF document.
     * @param filename the path to the new PDF document
     * @throws    DocumentException 
     * @throws    IOException 
     */
    public void createPdf(String filename)
    throws DocumentException, IOException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter.getInstance(document, new FileOutputStream(filename));
        // step 3
        document.open();
        // step 4
        document.add(new Paragraph("Hello World!"));
        // step 5
        document.close();
    }
}
4

3 に答える 3

8

明示的に指示しない限り、JVM /JDKlibまたはextフォルダーに何も追加しないでください。

開発環境(および将来の意図)に応じて、ライブラリをそれに最適な場所に配置する必要があります。たとえば、プロジェクトフォルダ内(ただしソース外)のlibディレクトリに配置する必要があります。

クラスパス依存関係をプロジェクトのJarマニフェストに追加するか(JARファイルのクラスパスへのクラスの追加を-cp確認してください) 、コマンドラインのパラメーターを使用してプログラムを実行する必要があります。プログラムをコンパイルするには、javacの-classpathオプションを使用する必要があります

開発環境に関しては、それはあなたが使用しているものに依存します

特記事項

各Jarファイルはクラスパスで個別に参照する必要があります。フォルダーを指定してJVMがその内容をスキャンしてJarファイルを探すことはできず、クラスでのみ機能します。

コンパイル実行例で更新

iText5.3.1をダウンロードします。私が抽出したzipファイルから:

  • itextpdf-5.3.1.jar
  • itext-pdfa-5.3.1.jar
  • itext-xtra-5.3.1.jar

そして、それらを簡単にアクセスできる場所に配置しました。

HelloWorldの例をiTextinActionのWebサイトからダウンロードしました。srcこれをJarと同じ場所のディレクトリに配置しました

結果のPDFが現在の作業ディレクトリに作成されるようにコードを変更しました

public static final String RESULT = "hello.pdf";

javac.exe -cp d:\hold\itextpdf-5.3.1.jar;d:\hold\itext-pdfa-5.3.1.jar;d:\hold\itext-xtra-5.3.1.jar -d . src\HelloWorld.java私は(でコンパイルされたd:\hold)で例をコンパイルしました

これにより、part1\chapter01にHelloWorldクラスが作成されました。D:\hold

次に、次の例を実行しましたjava -cp d:\hold\itextpdf-5.3.1.jar;d:\hold\itext-pdfa-5.3.1.jar;d:\hold\itext-xtra-5.3.1.jar;d:\hold part1.chapter01.HelloWorld

これによりhello.pdf、現在のディレクトリにが作成されました(D:\hold

于 2012-08-05T06:51:09.513 に答える
-1

これは、プロジェクトに itext ライブラリがないことが原因である可能性があります。itext jar ファイルをダウンロードしてプロジェクト ライブラリに追加するだけです。

于 2015-11-21T17:25:06.887 に答える