0

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();
    }
}
kalpha
  • 45
  • 7
  • 1
    to my knowledge if you need to have external jars, then you need a package manager i.e., maven or gradle. I suggest maven as a start. Its better to have an ide too. I'd suggest idea community edition. it is easy to setup and run – ahrooran Dec 28 '20 at 12:22
  • the reason why this error occurs is because java could not find the jar file of itext. Your location might be off. Try to give an absolute path as location. besides if you develop your code as a project/ have packages in it, you should definitely try an ide. That would be much easier – ahrooran Dec 28 '20 at 12:24
  • Try `javac -cp itext.jar C01E01_HelloWorld.java`. – vanje Dec 28 '20 at 12:25
  • @vanje itext 7 is not one-big-jar anymore, the code has been split into multiple jars. – mkl Dec 28 '20 at 13:19
  • updated initial thread with maybe some progress but no success via one attempt. I had attempted to follow a youtube itext helloworld with eclipse but I don't see the same options in my ecliplse as the vid had regarding the pom file. – kalpha Dec 28 '20 at 14:23
  • The last one is *almost* correct, you need to add the path where your compiled class is (which is presumably the local directory, in which case simply add `:.` to your `cp` switch). – Joachim Sauer Dec 28 '20 at 14:44
  • ooh ... more progress ... maybe so now I get Exception in thread "main" java.lang.NullPointerException at C01E01_HelloWorld.main(C01E01_HelloWorld.java:21) will post full source in main thread in case – kalpha Dec 28 '20 at 14:50
  • so by changing the string DEST back to a fully qualified path get's me further but now it complains Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory So now I need to fathom where sl4j should come from and what version is needed – kalpha Dec 28 '20 at 18:33

2 Answers2

0
  1. Install a decent IDE, like IntelliJ IDEA Community Edition.
  2. Clone the GitHub repository that contains the examples:
git clone https://github.com/itext/i7js-jumpstart
  1. In IntelliJ, open the pom.xml in the directory of the repo that you just cloned. IntelliJ will ask you if you want to open it as a Maven project.
  2. In IntelliJ, on the left, you have a tree view of all the files in the project. Go to the file src/main/java/tutorial/chapter01/C01E01_HelloWorld.java.
  3. Run the example (the main method).
Amedee Van Gasse
  • 7,280
  • 5
  • 55
  • 101
  • re 3. I get option to open as project or file no mention of maven? re 4. This appears with a banner saying on left Project JDK is not defined and on right Setup SDK. If I click on Setup SDK it says Detected SDK's java-11-openjdk version 11.0.9 default-jvm version 11.0.9 And a "Add SDK" option that then shows Download JDK JDK Intellij Platform Plugin SDK Android SDK In top right of C01E01_HelloWorld.java it has 10 errors and 3 warnings – kalpha Dec 29 '20 at 08:40
  • Open as project. And you may need to install a Maven extension in IntelliJ, better ask on the JetBrains forums to make sure. Or search on Stack Overflow. This is basic Java development and it is assumed that a seasoned Java developer already knows how to setup their development tools. – Amedee Van Gasse Dec 30 '20 at 01:41
  • thankyou, I will look into that, I've used for java for eons and never got along with it or it's disparate eco systems, really wish I could just gcc .... – kalpha Dec 30 '20 at 08:35
0

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

kalpha
  • 45
  • 7
  • 1
    But this is java 101: add all the required libraries to your classpath for running a program... – mkl Dec 29 '20 at 11:00
  • true, but they are iText library dependencies ... it took more searching to establish what they are called and where to get them. – kalpha Dec 29 '20 at 12:06
  • Well, the official iText documentation tells you that the simplest way to get started, is to use a dependency manager like Maven or Gradle. If you want to do it in a different way, it is assumed that you are a Java expert and know what you are doing... – Amedee Van Gasse Dec 30 '20 at 01:39
  • All true in some universe. I am not a Java expert and obviously don't know what I am doing, I tried following examples, tutorials and guidance for: maven, eclipse and IDEA and all failed so far.... For a Hello World example I don't think it should be that hard and should also be able to be done as simple as possible. If you pick up a library (or an alleged simple example) you should have the health warnings of it's specific dependencies. – kalpha Dec 30 '20 at 08:42
  • I'm sorry. I hope that someone else will be able to help you with that. – Amedee Van Gasse Jan 01 '21 at 18:33