install and compile iText under Windows
Hello, I installed JDK 1.5 and Tomcat 5.5 on a machine Windows, I also installed iText 2.0.1 (i get a jar file and zip source folder)so that I can print out pdf files, but when I compile the servelts containing the codes to print outpdf files, a message is posted saying that the classes called (Document, pdfWriter.....) do not exist.
Is it me which badly installed iText or there are other modifications to make? How can i proceed?
Help me
Here is the code
import java.io.IOException;
import java.util.Date;
import java.lang.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
publicclass testpdfextends HttpServlet{
privatestaticfinallong serialVersionUID = -6033026500372479591;
publicvoid doPost (HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException{
// step 1
Document document =new Document(PageSize.A4, 50, 50, 50, 50);
try{
// step 2: we set the ContentType and create an instance of the corresponding Writer
response.setContentType("application/pdf");
PdfWriter.getInstance(document, response.getOutputStream());
// step 3
document.open();
// step 4
document.add(new Paragraph("Hello World"));
document.add(new Paragraph(new Date().toString()));
}
catch(DocumentException de){
de.printStackTrace();
System.err.println("document: " + de.getMessage());
}
// step 5: we close the document (the outputstream is also closed internally)
document.close();
}
}
[3052 byte] By [
uxiana] at [2007-11-26 21:44:28]

# 1
Place the iText JAR in WEB-INF/lib of your web application.- Saish
# 2
Also, it doesn't hurt to verify that you unzip the iText download , and then copy only the JAR file to your /WEB-INF/lib/ folder or where ever your project's library folder is.
You can also place it under Tomcat's shared lib folder , if you are sharing library files accross different projects.
Message was edited by:
appy77
# 3
I like to know how to set the path and classpath to compile normal servelts(i mean other servlets) and servlets containing the iText codes.Please help me
# 4
The Path variavle will always point to the bin directory of you jdk installation
ie Path variable points to the location where the executables(exe`s) like java, javac etc are present
if you need extenal jar files in your code you should add them in the classpath
javac servlet.java -classpath .;C:\lib\servlet-api.jar;C:\lib\iText.jar
Message was edited by:
arunmp25
# 5
I copied the iText 2.0.1.jar into the folder /WEB-INF/lib , and i compile the servlet but it doesn't work.
This the path where i copied the .jar file :
C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\ROOT\WEB-INF\lib
This is how i set the path and classpath
C:\claire>set path=%path%;C:\Program Files\Java\jdk1.5.0_11\bin;
C:\claire>set CLASSPATH=.;C:\Program Files\Apache Software Foundation\Tomcat 5.5
\common\lib\servlet-api.jar;
claire is the folder containing the java file codes
then i compiled the servlet
C:\claire>javac testpdf.java
After compiling i got this message :
estpdf.java:6: package com.lowagie.text does not exist
import com.lowagie.text.Document;
^
testpdf.java:7: package com.lowagie.text does not exist
import com.lowagie.text.DocumentException;
^
testpdf.java:8: package com.lowagie.text does not exist
import com.lowagie.text.Paragraph;
^
testpdf.java:9: package com.lowagie.text.pdf does not exist
import com.lowagie.text.pdf.PdfWriter;
^
testpdf.java:18: cannot find symbol
symbol : class Document
location: class testpdf
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
^
testpdf.java:18: cannot find symbol
symbol : class Document
location: class testpdf
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
^
testpdf.java:18: cannot find symbol
symbol : variable PageSize
location: class testpdf
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
^
testpdf.java:22: cannot find symbol
symbol : variable PdfWriter
location: class testpdf
PdfWriter.getInstance(document, response.getOutputStream());
^
testpdf.java:27: cannot find symbol
symbol : class Paragraph
location: class testpdf
document.add(new Paragraph("Hello World"));
^
testpdf.java:28: cannot find symbol
symbol : class Paragraph
location: class testpdf
document.add(new Paragraph(new Date().toString()));
^
testpdf.java:30: cannot find symbol
symbol : class DocumentException
location: class testpdf
catch(DocumentException de) {
^
11 errors
Is it there any mistake ? help me how to proceed
# 6
No. No. No. Set the environment variable JAVA_HOME to your JDK installation. Make sure your PATH variable has JAVA_HOME/bin on it. Do *not* set the global CLASSPATH environment variable.
When you compile, you need to include itext.jar on your javac classpath (manually by setting it via -classpath or preferably have your IDE do it for you). When you deploy, you need to include itext.jar in WEB-INF/lib of your web application.
Tomcat itself needs to point to JAVA_HOME. But you rarely need to fiddle with this. If Tomcat successfully starts, it will normally be fine (other than a rare case where it is pointed at a JRE instead of a JDK and hence cannot compile JSP's at runtime).
- Saish
# 7
What saish explained is moretahn enough i just want to point this while setting classpath add itext.jar in classpathC:\claire>set CLASSPATH=.;C:\Program Files\Apache Software Foundation\Tomcat 5.5\common\lib\servlet-api.jar;Path to where you have placed iText.jar;
# 8
@ArunMP25: That is the right syntax for the CLASSPATH environment variable, but IMO, you should not really set this. JAVA_HOME already takes care of the JDK's location. Tomcat or whatever container you use should know how to find its own JAR's relative to the installation's root folder. That only leaves your own JARs, which in a J2EE application have proscribed locations. Even standalone applications do not need the global CLASSPATH. They can instead have the CLASSPATH specified via a shell or batch script. Just my two cents.
- Saish
# 9
saish i accept that you should not specify a global class pathI was suggesting to use the classpath via a shell or batch script.and you need to set the classpath while you are compiling servlets and when use 3rd party jar files
# 10
> saish i accept that you should not specify a global
> class path
> I was suggesting to use the classpath via a shell or
> batch script.
>
> and you need to set the classpath while you are
> compiling servlets and when use 3rd party jar files
It all depends on what tool you use to compile your Servlets and 3rd party JAR files.
If you use Ant , then one would simply specify the classpath used by the project in Ant's build.xml folder
If you use an IDE to compile , the specify the classes folder and JAR files in the IDE's classpath.
If you use javac from the command line , then you can use the -classpath option on the javac command , or you could set it the CLASSPATH in the environment variable.
But generally speaking setting a global / system CLASSPATH is highly discouraged.
Why? because if you had multiple projects on the same machine and each project used different versions of JAR files, then if you used a system wide CLASSPATH it would raise conflicts. That's why its better to have a project specific classpath that can be speicified in ant's build or other tools you use to compile your classes.
Only specify JAVA_HOME , CATALINA_HOME (if you're using Tomcat), ANT_HOME (if you're using Ant) , in the System Environment Variables settings.
# 11
> saish i accept that you should not specify a global
> class path
> I was suggesting to use the classpath via a shell or
> batch script.
>
Fair enough. My bad. :^)
> and you need to set the classpath while you are
> compiling servlets and when use 3rd party jar files
Need to? I still can't set those in a shell script? :^)
- Saish