So I’ve downloaded the compiled itext jar files from https://github.com/itext/itext7/releases/latest and placed them in same folder as the iText example C01E01_HelloWorld.java but when I run
javac C01E01_HelloWorld.java
I get
$ javac C01E01_HelloWorld.java C01E01_HelloWorld.java:3: error: package com.itextpdf.kernel.pdf does not exist import com.itextpdf.kernel.pdf.PdfDocument; ^ C01E01_HelloWorld.java:4: error: package com.itextpdf.kernel.pdf does not exist import com.itextpdf.kernel.pdf.PdfWriter; ^ C01E01_HelloWorld.java:5: error: package com.itextpdf.layout does not exist import com.itextpdf.layout.Document; ^ C01E01_HelloWorld.java:6: error: package com.itextpdf.layout.element does not exist import com.itextpdf.layout.element.Paragraph; ^ C01E01_HelloWorld.java:25: error: cannot find symbol PdfWriter writer = new PdfWriter(dest); ^ symbol: class PdfWriter location: class C01E01_HelloWorld C01E01_HelloWorld.java:25: error: cannot find symbol PdfWriter writer = new PdfWriter(dest); ^ symbol: class PdfWriter location: class C01E01_HelloWorld C01E01_HelloWorld.java:28: error: cannot find symbol PdfDocument pdf = new PdfDocument(writer); ^ symbol: class PdfDocument location: class C01E01_HelloWorld C01E01_HelloWorld.java:28: error: cannot find symbol PdfDocument pdf = new PdfDocument(writer); ^ symbol: class PdfDocument location: class C01E01_HelloWorld C01E01_HelloWorld.java:31: error: cannot find symbol Document document = new Document(pdf); ^ symbol: class Document location: class C01E01_HelloWorld C01E01_HelloWorld.java:31: error: cannot find symbol Document document = new Document(pdf); ^ symbol: class Document location: class C01E01_HelloWorld C01E01_HelloWorld.java:34: error: cannot find symbol document.add(new Paragraph("Hello World!")); ^ symbol: class Paragraph location: class C01E01_HelloWorld 11 errors
I also tried
javac -cp /home/user01/itext/demo/ C01E01_HelloWorld.java
i.e. where the -cp points to where the iText jar’s (and C01E01_HelloWorld.java) is with same result. How do I get the import lines to know and use the iText jar files?
Or can this not be done in a simple way and need eclipse or maven or ?
So I ran the following without error
javac -cp kernel-7.1.13.jar:layout-7.1.13.jar:io-7.1.13.jar C01E01_HelloWorld.java
if I left any of the jar files out it resulted in errors related to the missing jar. but when I try
java C01E01_HelloWorld
I get
Error: Unable to initialize main class C01E01_HelloWorld Caused by: java.lang.NoClassDefFoundError: com/itextpdf/layout/element/IBlockElement
I also tried
java -cp kernel-7.1.13.jar:layout-7.1.13.jar:io-7.1.13.jar C01E01_HelloWorld
which gave slightly different
Error: Could not find or load main class C01E01_HelloWorld Caused by: java.lang.ClassNotFoundException: C01E01_HelloWorld
so next step yields
$ java -cp kernel-7.1.13.jar:layout-7.1.13.jar:io-7.1.13.jar:. C01E01_HelloWorld Exception in thread "main" java.lang.NullPointerException at C01E01_HelloWorld.main(C01E01_HelloWorld.java:21)
my source is
/*package tutorial.chapter01;*/ import com.itextpdf.kernel.pdf.PdfDocument; import com.itextpdf.kernel.pdf.PdfWriter; import com.itextpdf.layout.Document; import com.itextpdf.layout.element.Paragraph; import java.io.File; import java.io.IOException; /** * Simple Hello World example. */ public class C01E01_HelloWorld { /* public static final String DEST = "results/chapter01/hello_world.pdf"; */ public static final String DEST = "hello_world.pdf"; public static void main(String args[]) throws IOException { File file = new File(DEST); file.getParentFile().mkdirs(); new C01E01_HelloWorld().createPdf(DEST); } public void createPdf(String dest) throws IOException { //Initialize PDF writer PdfWriter writer = new PdfWriter(dest); //Initialize PDF document PdfDocument pdf = new PdfDocument(writer); // Initialize document Document document = new Document(pdf); //Add paragraph to the document document.add(new Paragraph("Hello World!")); //Close document document.close(); } }
Advertisement
Answer
So I downloaded slf4j.api, slf4j-log4j12, and log4j using links provided in
SLF4J: Which .jar files does iText7 need exactly?
and also downloaded the compiled itext jar files from
https://github.com/itext/itext7/releases/latest
I then ran
javac -cp kernel-7.1.13.jar:layout-7.1.13.jar:io-7.1.13.jar:slf4j.api-1.6.1.jar:slf4j-log4j12-1.6.1.jar:log4j-1.2.16.jar C01E01_HelloWorld.java
and then
java -cp kernel-7.1.13.jar:layout-7.1.13.jar:io-7.1.13.jar:slf4j.api-1.6.1.jar:slf4j-log4j12-1.6.1.jar:log4j-1.2.16.jar:. C01E01_HelloWorld
and finally I get the alleged simple hello world pdf….
I’ve tried maven, eclipse, and now IDEA and failed to get any to “just” work following tutorials or hints …. yet