Sunday, February 3, 2019

java - Don't understand why I'm getting the Error: Could not find or load main class from console




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!


Answer



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


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...