0

Context: This is my first time working with java and importing jar files etc as this is for a school assignment.

Im pretty sure I have included all the jar files needed as I got them directly from the github page for iText7. I only intended to test this code to see if it would create a pdf file with the title and empty table inside but then I got this error which was pretty unexpected. I have 0 clue how to resolve it or what half of it even means.

This is the error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
    at com.itextpdf.commons.actions.ProductEventHandler.<clinit>(ProductEventHandler.java:52)
    at com.itextpdf.commons.actions.EventManager.<init>(EventManager.java:42)
    at com.itextpdf.commons.actions.EventManager.<clinit>(EventManager.java:37)
    at com.itextpdf.kernel.pdf.PdfDocument.open(PdfDocument.java:1950)
    at com.itextpdf.kernel.pdf.PdfDocument.<init>(PdfDocument.java:273)
    at com.itextpdf.kernel.pdf.PdfDocument.<init>(PdfDocument.java:254)
    at com.assignment.src.entities.report.ReportImpl.generateMonthlyReport(ReportImpl.java:182)
    at com.assignment.src.entities.shared.Tests.main(Tests.java:83)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
    ... 8 more

The code in question:

        public void generateMonthlyReport(LocalDateTime date) throws IOException {
            StaffImpl si = new StaffImpl();
            ArrayList<String[]> reportByMonth = this.getByMonth(date);
            ArrayList<String[]> allTrainer = si.getByRole(Role.Trainer);
            PdfWriter pw = new PdfWriter(FileSelection.MonthlyReport.toString());
            PdfDocument pdf = new PdfDocument(pw);
            Document doc = new Document(pdf);
            PdfFont bold = PdfFontFactory.createFont(String.valueOf(FontStyles.BOLD));
    
    //      Adding Title
            Paragraph title = new Paragraph("Monthly Report\n\n");
            title.setFont(bold);
            doc.add(title);
    
    //      Adding Table
            float[] columnWidths = {150F, 150F, 150F, 150F, 150F, 150F};
            Table table = new Table(columnWidths);
    
            table.addCell("Trainer ID").setFont(bold);
            table.addCell("Trainer Name").setFont(bold);
            table.addCell("Sales").setFont(bold);
            table.addCell("Commission").setFont(bold);
            table.addCell("Revenue").setFont(bold);
            table.addCell("Profit").setFont(bold);
    
            doc.add(table);
            doc.close();
Xyds
  • 3
  • 2
  • 2
    please add your pom.xml or gradle file here.. i think dependency missing is slf4j and log4j2 jars in project due to that error is there – Vikrant Kashyap May 20 '22 at 09:48
  • Sorry but im not too familiar with java. Im not currently using maven or any frameworks for that matter in my project. – Xyds May 20 '22 at 11:11
  • 1
    First advice would be to definitely look into Maven and Gradle. Including jar files manually is a recipe for disaster. Second advice is, if you only want to test out some iText 7 Code is to do it on the [product website itself](https://itextpdf.com/en/products/itext-7/itext-7-core). – André Lemos May 20 '22 at 11:25
  • Also see [this question](https://stackoverflow.com/q/49410540/1729265). – mkl May 20 '22 at 13:12
  • We'd need to know how you attempted to add the JAR to the project. Typically, this is done with a build tool, such as Maven, Gradle, or Ant. We don't have enough information with what you've included in this question. The code itself is most likely not relevant in this situation and instead we'd need to know how SLF4J (and iText) is being added to your project's classpath. – JNYRanger May 20 '22 at 16:52
  • I've included the jar files from the comment by @mkl and now its working fine. Prior to that, I had only added the itext7 jars without the slf4j and bouncy castle jars. – Xyds May 21 '22 at 07:11
  • Regarding @JNYRanger 's comment, I'm using intellij and I added the jars via File -> Project Structure -> Modules -> Dependencies, selecting the folders in which I had stored my jar files. – Xyds May 21 '22 at 07:11
  • 1
    *"I've included the jar files from the comment by @mkl and now its working fine"* - great! So your question essentially is a duplicate of the referenced one. I'll close it as such. – mkl May 21 '22 at 08:36

0 Answers0