0

I have looked over several other answers on this site trying to understand why this might be happening, but I don't understand what I'm doing wrong.

I am trying to get started with iText, and .jar files in general. I downloaded and extracted the iText .jar files to a folder on my desktop: Desktop\Java\itext-5.4.4\"jar files here"

I then went to the following site:

http://tutorials.jenkov.com/java-itext/getting-started.html

and copied the code into Notepad. It looks like this:

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

import java.io.FileOutputStream;
import java.io.FileNotFoundException;

/**

 */
public class HelloWorldExample {

    public static void main(String[] args) {

        Document document = new Document();

        try {
            PdfWriter.getInstance(document,
                new FileOutputStream("HelloWorld.pdf"));

            document.open();
            document.add(new Paragraph("A Hello World PDF 

document."));
            document.close(); // no need to close 

PDFwriter?

        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }
}

I saved the file on my desktop as HelloWorldExample.java

I then went to compile my code with the following commands:

cd c:\desktop

javac -classpath Java\itext-5.4.4\itextpdf-5.4.4.jar HelloWorldExample.java

This compiled successfully

I then tried:

java -classpath Java\itext-5.4.4\itextpdf-5.4.4.jar HelloWorldExample

And I get the Error: Could not find or load main class HelloWorldExample error.

I have tried many variations on this including making a folder, placing a lib folder in that folder, and creating a package, but still get the same error.

What is happening here?

Thanks!

JHM
  • 61
  • 1
  • 6
  • Are you sure HelloWorldExample.class is in classpath specified by -classpath option of java command? – crybird Oct 22 '13 at 07:58
  • @crybird My HelloWorldExample.class is not in that directory specified by the classpath command - it is compiled to the same directory as the .java file, the desktop. I tried picking the .class file up and moving it to that folder - same error. I'm just using the commands suggested by [this](http://stackoverflow.com/questions/5112607/how-to-include-libraries-in-java-without-using-an-ide), which seems to suggest this - I don't know if there is some system variable causing a problem or what. – JHM Oct 22 '13 at 08:15
  • 1
    The classpath you specified must contain your complied class. – crybird Oct 22 '13 at 10:00

1 Answers1

5

You tried:

java -classpath Java\itext-5.4.4\itextpdf-5.4.4.jar HelloWorldExample

And you get the Error: Could not find or load main class HelloWorldExample error.

HelloWorldExample is sought for in the class path. You explicitly set the classpath to merely include the iText jar in your Java call. As the iText jar surely does not include your HelloWorldExample, it obviously cannot be found.

I assume you work on some Windows OS (considering your choice of path separators). Thus you should try something like this to include the current directory c:\desktop in your Java call:

java -classpath Java\itext-5.4.4\itextpdf-5.4.4.jar;. HelloWorldExample

or

java -classpath Java\itext-5.4.4\itextpdf-5.4.4.jar;c:\desktop HelloWorldExample
mkl
  • 90,588
  • 15
  • 125
  • 265
  • This works, thanks - however, the colon is not needed on the javac command - which I think is weird. – JHM Oct 23 '13 at 17:00
  • When you compiled that source file, its file name was argument of the call. When you ran it, it's not a file name anymore but a class name to find somewhere. – mkl Oct 23 '13 at 21:24