2

I just finished programming my new project. It should deliver me some questionnaires concerning Handball. I saved all questions in a HSQL Database and want to export them in like random order to a PDF. Everything workes 100% fine in my development environment. I can connection to my HSQL Database via the provided Library and I can build some PDF documents via the iText Library Version 5.3.5.

But once I export my project as a runnable JAR-File (Libraries in a Folder) I cannot build PDF's anymore. Ridiculously I can connect to my database but cannot build PDFS. I think my JAR-File cannot connect to the iText Library.

Is that the matter ? And if, what can I do to change it? What else could it be ?

In the following im going to post my DBConnection class and my PDFCreator class:

PDFCreator:

´int maxiID = db.ExecuteMaxiID(katalog);
            int anzahl1 = anzahl;
            int kontr = 0;
            int pageNumber = 0;

            if (anzahl == 0) {
                anzahl = maxiID;
                anzahl1 = 0;
            }

            int[] done = new int[anzahl]; // anzahl 
            for (int x = 0; x < anzahl; x++) {
                done[x] = x + 1;
            }

            doc.open();
            doc.addCreationDate();
            Paragraph space = new Paragraph();

            // Image oben rechts
            Image img = Image.getInstance("icon.png");
            img.scaleAbsoluteHeight(40);
            img.scaleAbsoluteWidth(110);
            img.getScaledWidth();
            img.setAbsolutePosition(460, 780);
            doc.add(img);
            space.add(" ");

            // Titel

            PdfPTable tittle = new PdfPTable(1);
            tittle.setWidthPercentage(100);

            PdfPCell titleCell = new PdfPCell(new Phrase("Handball-Fragen-Test", new Font(Font.FontFamily.TIMES_ROMAN, 20, Font.BOLD)));
            titleCell.setHorizontalAlignment(Element.ALIGN_CENTER);
            titleCell.setBorder(Rectangle.NO_BORDER);
            PdfPCell name = new PdfPCell(new Phrase("Name: "));
            name.setBorder(Rectangle.NO_BORDER);

            tittle.addCell(titleCell);
            tittle.addCell(name);
            doc.add(tittle);

            doc.add(space);
            doc.add(space);
            doc.add(space);

            // Eigentliche Fragen

            String[][] liste = new String[7][anzahl]; // anzahl
            liste = db.ExecuteGetSomeQuestions(anzahl1, katalog);// Anzahl katalog

            PdfPTable table = new PdfPTable(2);
            int[] wid = {1, 30};
            try {
                table.setWidths(wid);
            } catch (DocumentException e) {

            }
            table.setLockedWidth(false);
            table.setWidthPercentage(100);
            table.setKeepTogether(true);

            PdfPCell frage;
            PdfPCell a1;
            PdfPCell a2;
            PdfPCell a3;
            PdfPCell platz;
            PdfPCell smallSpace;

            MyFooter event = new MyFooter();
            writer.setPageEvent(event);

            for (int i = 0; i < anzahl; i++) {
                int count = i + 1;
                platz = new PdfPCell(new Phrase(" "));
                platz.setColspan(2);
                platz.setBorder(Rectangle.NO_BORDER);

                smallSpace = new PdfPCell(new Phrase(" "));
                smallSpace.setBorder(Rectangle.NO_BORDER);

                List frageliste = new List(List.UNORDERED);
                ListItem item = new ListItem(liste[1][i]);
                frageliste.setListSymbol(count + ". ");
                item.setAlignment(Element.ALIGN_UNDEFINED);
                item.setLeading(15);
                frageliste.add(item);

                List a1list = new List(List.UNORDERED);
                a1list.setListSymbol("a) ");
                ListItem item1 = new ListItem(liste[2][i]);
                item1.setAlignment(Element.ALIGN_UNDEFINED);
                a1list.add(item1);

                List a2list = new List(List.UNORDERED);
                a2list.setListSymbol("b) ");
                ListItem item2 = new ListItem(liste[3][i]);
                item2.setAlignment(Element.ALIGN_UNDEFINED);
                item.setLeading(15);
                a2list.add(item2);

                List a3list = new List(List.UNORDERED);
                a3list.setListSymbol("c) ");
                ListItem item3 = new ListItem(liste[4][i]);
                item3.setAlignment(Element.ALIGN_UNDEFINED);
                item.setLeading(15);
                a3list.add(item3);

                frage = new PdfPCell();
                frage.addElement(frageliste);
                frage.setColspan(2);
                frage.setBorder(Rectangle.NO_BORDER);
                frage.setHorizontalAlignment(Element.ANCHOR);

                a1 = new PdfPCell();
                a1.addElement(a1list);
                a1.setBorder(Rectangle.NO_BORDER);

                a2 = new PdfPCell();
                a2.addElement(a2list);
                a2.setBorder(Rectangle.NO_BORDER);

                a3 = new PdfPCell();
                a3.addElement(a3list);
                a3.setBorder(Rectangle.NO_BORDER);

                table.addCell(frage);
                table.addCell(smallSpace);
                table.addCell(a1);
                table.addCell(smallSpace);
                table.addCell(a2);
                table.addCell(smallSpace);
                table.addCell(a3);
                table.addCell(platz);
                table.addCell(platz);

                doc.add(table);

                table.deleteBodyRows();


                // Hier füge ich die Seitenzahl hinzu.


                pageNumber = doc.getPageNumber();
                for (int j = 0; j < anzahl; j++) {
                    // Das Hier mache ich, da das hier eine for-Schleife ist und jede Frage als eine neue Tabelle hinzugefügt wird
                    // Dafür ist der Vergleich mit der Liste "done" da, damit jede Seitenzahl nur einmal hinzugefügt
                    if (done[j] == pageNumber) {
                        MyFooter event1 = new MyFooter();
                        writer.setPageEvent(event1);
                        done[j] = 0;
                    }
                }
            }

            // Der leere Lösungsbogen

            doc.newPage();

            PdfPTable titLoes = new PdfPTable(1);
            titLoes.setWidthPercentage(100);

            PdfPCell titLoesCell = new PdfPCell(new Phrase("Lösungsbogen", new Font(Font.FontFamily.TIMES_ROMAN, 20, Font.BOLD)));
            titLoesCell.setHorizontalAlignment(Element.ALIGN_CENTER);
            titLoesCell.setBorder(Rectangle.NO_BORDER);

            titLoes.addCell(titLoesCell);
            titLoes.addCell(name);
            doc.add(titLoes);

            doc.add(space);
            doc.add(space);
            doc.add(space);

            PdfPTable loesTab = new PdfPTable(6);
            loesTab.setLockedWidth(false);
            loesTab.setWidthPercentage(80);
            loesTab.getDefaultCell().setVerticalAlignment(Element.ALIGN_CENTER);
            loesTab.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);

            int[] widths = {1, 2, 2, 2, 3, 4};
            try {
                loesTab.setWidths(widths);
            } catch (DocumentException e) {

            }

            PdfPCell count = new PdfPCell();
            PdfPCell hell = new PdfPCell();
            BaseColor bC = new BaseColor(245, 245, 245);
            BaseColor bC2 = new BaseColor(220, 220, 220);

            loesTab.addCell(count);
            loesTab.addCell("A");
            loesTab.addCell("B");
            loesTab.addCell("C");
            loesTab.getDefaultCell().setBackgroundColor(bC2);
            loesTab.addCell("");
            loesTab.getDefaultCell().setBackgroundColor(BaseColor.WHITE);
            loesTab.addCell(hell);

            for (int go = 0; go < anzahl; go++) {
                int ind = go + 1;
                if (ind % 2 == 0) {
                    loesTab.getDefaultCell().setBackgroundColor(bC);
                }
                loesTab.addCell("" + ind);
                loesTab.addCell("");
                loesTab.addCell("");
                loesTab.addCell("");
                loesTab.getDefaultCell().setBackgroundColor(bC2);
                loesTab.addCell("");
                loesTab.getDefaultCell().setBackgroundColor(BaseColor.WHITE);
                if (ind % 2 == 0) {
                    loesTab.getDefaultCell().setBackgroundColor(bC);
                }
                loesTab.addCell("");
                loesTab.getDefaultCell().setBackgroundColor(BaseColor.WHITE);
            }

            doc.add(loesTab);

            // Die Lösungen

            doc.newPage();

            PdfPTable titLoes1 = new PdfPTable(1);
            titLoes1.setWidthPercentage(100);

            PdfPCell titLoesCell1 = new PdfPCell(new Phrase("Lösung", new Font(Font.FontFamily.TIMES_ROMAN, 20, Font.BOLD)));
            titLoesCell1.setHorizontalAlignment(Element.ALIGN_CENTER);
            titLoesCell1.setBorder(Rectangle.NO_BORDER);

            titLoes1.addCell(titLoesCell1);
            doc.add(titLoes1);

            doc.add(space);
            doc.add(space);
            doc.add(space);

            PdfPTable loesTab1 = new PdfPTable(7);
            loesTab1.setLockedWidth(false);
            loesTab1.setWidthPercentage(80);
            loesTab1.getDefaultCell().setVerticalAlignment(Element.ALIGN_CENTER);
            loesTab1.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);

            int[] widths1 = {1, 2, 2, 2, 2, 3, 4};
            try {
                loesTab1.setWidths(widths1);
            } catch (DocumentException e) {

            }

            loesTab1.addCell(count);
            loesTab1.addCell("Ur. ID");
            loesTab1.addCell("A");
            loesTab1.addCell("B");
            loesTab1.addCell("C");

            Font font = new Font(Font.FontFamily.UNDEFINED, 9);

            loesTab1.getDefaultCell().setBackgroundColor(bC2);
            loesTab1.addCell("");
            loesTab1.getDefaultCell().setBackgroundColor(BaseColor.WHITE);
            loesTab1.addCell(hell);

            for (int go = 0; go < anzahl; go++) {

                boolean A = false;
                boolean B = false;
                boolean C = false;

                String loesung = liste[5][go];
                int lenResult = 0;
                String[] result = null;
                if (loesung.contains(" ") == true) {
                    String[] zwischen = loesung.split(" ");
                    int lenZwischen = zwischen.length;
                    loesung = "";
                    for (int i = 0; i < lenZwischen; i++) {
                        loesung += zwischen[i];
                    }

                }

                result = loesung.split(",");
                lenResult = result.length;

                int ind = go + 1;
                if (ind % 2 == 0) {
                    loesTab1.getDefaultCell().setBackgroundColor(bC);
                }
                loesTab1.addCell("" + ind);
                Paragraph urid = new Paragraph();
                urid.setFont(font);
                urid.add("" + liste[6][go]);
                loesTab1.addCell(urid);

                for (int i = 0; i < lenResult; i++) {
                    if (result[i].equals("a") == true) {
                        A = true;
                    }
                    if (result[i].equals("b") == true) {
                        B = true;
                    }
                    if (result[i].equals("c") == true) {
                        C = true;
                    }
                }
                if (A == true) {
                    loesTab1.addCell("X");
                } else {
                    loesTab1.addCell("");
                }
                if (B == true) {
                    loesTab1.addCell("X");
                } else {
                    loesTab1.addCell("");
                }
                if (C == true) {
                    loesTab1.addCell("X");
                } else {
                    loesTab1.addCell("");
                }
                loesTab1.getDefaultCell().setBackgroundColor(bC2);
                loesTab1.addCell("");
                loesTab1.getDefaultCell().setBackgroundColor(BaseColor.WHITE);
                if (ind % 2 == 0) {
                    loesTab1.getDefaultCell().setBackgroundColor(bC);
                }
                loesTab1.addCell("");
                loesTab1.getDefaultCell().setBackgroundColor(BaseColor.WHITE);
            }

            doc.add(loesTab1);
            doc.close();

            //System.out.println("Finished");
            String retit = "PDF gespeichert";
            return retit;
        } catch (DocumentException | IOException e) {
            String retit = "Fehler beim abspeichern";
            return retit;
        }
    }`

DBConnection:

public void GetConnection() {
    System.out.println("GOT CONNECTION");
    // Objekt von Klasse Pfad erstellen, um den absoluten Pfad der Datenbank zu bekommen (im falle Sie Kopieren das Programm an einen anderen Speicherort)
    Pfad path = new Pfad();
    try {

        // Es wird versucht der JDBC Treiber für HSQL aus der von mir importierten Bibliothek zu laden
        Class.forName("org.hsqldb.jdbcDriver");
    } catch (ClassNotFoundException e) {
        //PopUp //puElement = new //PopUp();
        //puElement.DrawPopUp("Fehler beim Laden von Treiber");
    }

    con = null;

    try {
        try {
            // Nun wird versucht mithilfe des Treibers eine Connection aufzubauen
            con = DriverManager.getConnection("jdbc:hsqldb:file:" + path.Wo() + "; shutdown = true", "TKuschel", "");

        } catch (IOException | SQLException e) {
            //PopUp //puElement = new //PopUp();
            //puElement.DrawPopUp("Datei nicht gefunden");
        }
    } catch (Exception e) {
        //PopUp //puElement = new //PopUp();
        //puElement.DrawPopUp("Fehler mit der Datenbank");
    }
}

public void CloseConnection() {
    // Diese Methode ist wichtig, da Änderungen erst nach schließen der Connection in der Datenbank angewendet werden. Bis dorthin sind sie nur im Zugehörigen
    // ResultSet gespeichert
    try {
        con.close();
    } catch (SQLException e) {
        //PopUp //puElement = new //PopUp();
        //puElement.DrawPopUp("Fehler beim schließen der Datenbank");
    }
}



public String[][] GetSomeQuestions(int anzahl, String Katalog) {
    // Deklarieren der Variablen

    avaliableFragen.removeAll(avaliableFragen);
    fragenIndex.removeAll(fragenIndex);

    boolean kontrol = true;
    int size = 0;
    int columns = 0;
    int maxiID = 0;

    CloseGaps(Katalog);

    if (anzahl > 0) {
        Random roller = new Random();
        int maxID = MaxiID(Katalog);
        int fuerRoller = maxID + 1;
        // Hier bespiele ich die ArrayList avaliableFragen mit den ID's aller möglichen Fragen
        for (int i = 0; i < maxID;) {
            i = i + 1;
            avaliableFragen.add(i);
        }
        while (kontrol == true) {
            int rand = roller.nextInt(fuerRoller);
            if (avaliableFragen.contains(rand) == true) {
                int delete = avaliableFragen.indexOf(rand);
                fragenIndex.add(rand);
                avaliableFragen.remove(delete);
               /* Hier würfele ich mit der Methode "nextInt(int)" der Klasse java.util.Random einen Wert zwischen 1 und maxID (einbegriffen)
        * Da ich jede Frage nur einmal im Fragebogen haben möchte (sie wahrscheinlich auch) schaue ich ob die ID in meiner avaliableFragen liste
                * vorhanden ist. Ist dem so, wird sie fragenIndex hinzugefügt und aus avaliableFragen gelöscht. Ist dem nicht so, und die gewürfelte ID
                * ist in avaliableFragen nicht mehr vorhanden, wird sie auch fragenIndex nicht hinzugefügt und es wird eine neue ID gewürfelt.  
                */
            }
            if (avaliableFragen.isEmpty() == true) {
                kontrol = false;
            }
            if (fragenIndex.size() == anzahl) {
                kontrol = false;
            }
            // Es soll solange gehen bis entweder die avaliableFragen liste leer ist oder die gewünschte Anzahl erreicht ist. 
            // Das es aufhören soll, wenn avaliableFragen leer ist, habe ich eingebaut, damit es keinen Fehler wirft, wenn eine Falsche angabe gemacht wurde.
        }
        size = fragenIndex.size();
    }
    if (anzahl == 0) {
        // Es gibt eine Funktion sich den kompletten Katalog als Pdf, sortiert ausgeben zu lassen, damit man herausfinden kann, welche Fragen man löschen möchte
        // suchen kann ob eine bestimmte Frage vorhanden ist etc.
        // Dazu wird beim Drücken eines Buttons (KatalogBearbeiten Button "BrowseSpeicherort") der Wer 0 für die Anzahl weitergegeben. In dem Fall 
        // passiert eben das Folgende.

        CloseGaps(Katalog);
        maxiID = MaxiID(Katalog);
        for (int i = 0; i <= maxiID;) {
            i = i + 1;
            fragenIndex.add(i);
        }
        size = maxiID;
    }

    // Hier werden die Listen initialisiert, die ich später auch returnen werde. Ihnen werden die Werte der Reihen im folgendn try-catch Block übergeben
    String[][] FragKat1 = new String[7][size];
    String[][] FragKat2 = new String[10][size];

    try {

        for (int i = 0; i < size; i++) {
            String sql = ("SELECT * FROM " + Katalog + " WHERE ID = " + fragenIndex.get(i));
            ResultSet rs = ExecuteGetSQL(sql);
            while (rs.next()) {
                columns = rs.getMetaData().getColumnCount();
                if (columns == 7) {
                    FragKat1[0][i] = rs.getString(1);
                    FragKat1[1][i] = rs.getString(2);
                    FragKat1[2][i] = rs.getString(3);
                    FragKat1[3][i] = rs.getString(4);
                    FragKat1[4][i] = rs.getString(5);
                    FragKat1[5][i] = rs.getString(6);
                    FragKat1[6][i] = rs.getString(7);
                }
                if (columns == 10) {
                    FragKat2[0][i] = rs.getString(1);
                    FragKat2[1][i] = rs.getString(2);
                    FragKat2[2][i] = rs.getString(3);
                    FragKat2[3][i] = rs.getString(4);
                    if (!"leer".equals(rs.getString(4))) {
                        FragKat2[4][i] = rs.getString(5);
                    }
                    if (!"leer".equals(rs.getString(6))) {
                        FragKat2[5][i] = rs.getString(6);
                    }
                    if (!"leer".equals(rs.getString(7))) {
                        FragKat2[6][i] = rs.getString(7);
                    }
                    if (!"leer".equals(rs.getString(8))) {
                        FragKat2[7][i] = rs.getString(8);
                    }
                    FragKat2[8][i] = rs.getString(9);
                    FragKat2[9][i] = rs.getString(10);
                }
            }
            rs.close();
        }
    } catch (SQLException w) {

    }
    if (columns == 7) {
        return FragKat1;
    }
    if (columns == 10) {
        return FragKat2;
    }
    return null;

Don't hesitate to comment if you require some other information. Please help me I'm kinda desperate.

1 Answers1

0

Without further clarification, this is a pretty broad error. Likely as you have stated it is a dependency issue. If you are using maven, maybe it isn't configured properly on the prod. pc, and it can't download the appropriate jar from the central repo.

If you are not using any build tools, Manuel's answer will be the good approach.

Peterk.
  • 3
  • 3
  • We can kinda close it here I think. I just found the mistake. And it rly was a simple one.... I added some image to my pdfs but forgot to put this into the jar folder so it threw an error. But thanks nontheless guys youre the greatest – skt 1ntod4rkness May 06 '17 at 17:56
  • I'm glad you succeded. To really close it, either accept my answer, or post this comment as answer and accept that. Might help somebody in the future. – Peterk. May 08 '17 at 08:03