Gomathi
Gomathi

Reputation: 1003

Error in itext package

I downloaded itext-1.3 and placed into lib folder of jdk 1.6.0 . And added the lib folder as the CLASSPATH in system variables.

But while I run the program I get the error:

package com.itextpdf.text does not exist.

Similarly for all other packages too. What mistake I made?

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();
    }
}

Upvotes: 3

Views: 25269

Answers (3)

Jay
Jay

Reputation: 11

Don't need to do anything on jdk library. Just need to give path of itext jar files, to the java by using classpath.

I also suffer from the same problem but having itext 5.5.3. In my case I create a lib folder in parallel to my working folder. In lib folder I put all the three jar files (itextpdf-5.5.3.jar, itext-pdfa-5.5.3.jar, itext-xtra-5.5.3.jar). Don't waste time in giving the path of each jar file separately at the time of compiling and running the code.

I compiled the code, eg1.java(c:\ItextPractise\code\eg1.java) with

javac -classpath c:\ItextPractise\lib\*;. eg1.java

and run using

java -classpath c:\ItextPractise\lib\*;. eg1 (eg1 is the name of main class in my case)

You may know more about classpath from OracleOfficialPage.

Upvotes: 1

shehu abdul
shehu abdul

Reputation: 1

this might be as a result of the absence of the itext library in your project. simply download the itext jar file and add it to your project library

Upvotes: -1

MadProgrammer
MadProgrammer

Reputation: 347214

You shouldn't add anything to the JVM/JDK lib or ext folders unless you instructed to do so explicitly

Depending on you development environment (& your future intentions), you should place the libraries in a location best suited to it, for example, in a lib directory within you project folder (but outside of the source).

You should either add a class-path dependence into the projects Jar manifest (check out Adding Classes to JAR File's Classpath) or use the -cp parameter on the command line to execute the program. You should use the -classpath option of the javac to compile the program

As for the development environment, that depends on what you are using

Special note

Each Jar file needs to be referenced separately on the Classpath, you can not specify a folder & expect the JVM to scan its contents for Jar files, only works for classes

UPDATED WITH COMPILE EXECUTE EXAMPLE

I download iText 5.3.1. From the zip file I extracted:

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

And placed them in an easy to reach location.

I downloaded the HelloWorld example from the iText in Action website. I placed this in the src directory under the same location as the Jar's

I modified the code so that the resulting PDF would be created in the current working directory

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

I compiled the example with 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 (compiled in d:\hold)

This created the HelloWorld class in part1\chapter01 in D:\hold

I then executed the example with 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

This resulted in the creation of hello.pdf in the current directory (D:\hold)

Upvotes: 8

Related Questions