0

I'm creating a simple project that can convert PDF files to Excel files using GroupDocs Converter. Running it in Eclipse works, but when I try to run it from cmd, errors start to pop-up on my screen. I'm not a professional informatic, I'm only an highschooler so I can't ask you to trust me when I say that the code is perfect and it works, so there it is.

package application;

import java.io.File;
import java.util.List;

import com.groupdocs.conversion.Converter;
import com.groupdocs.conversion.options.convert.SpreadsheetConvertOptions;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.paint.ImagePattern;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

public class App extends Application {

    static Stage primaryStage;
    static List<File> file;
    static String dir;
    static String pdfFileName;
    static String excelFileName;
    static Text filesChoosed = new Text(
            "FILE SCELTI CARICATI, CLICCA IL SIMBOLO EXCEL E ASPETTA FINO A FINE CONVERSIONE.");
    static Text completation = new Text("CONVERSIONE COMPLETATA.");
    static VBox boxes;

    /**
     * Metodo start, dove lo stage viene configurato
     */
    public void start(Stage primaryStage) throws Exception {

        // Costruzione iniziale della scena
        primaryStage.setTitle("PDF to XLSX Convertor");
        Rectangle pdfLogo = new Rectangle(110, 110);
        Rectangle buttonConversion = new Rectangle(110, 110);
        pdfLogo.setFill(new ImagePattern(new Image(
                "https://th.bing.com/th/id/R.dd78555980adf25273aee54a1c021ddb?rik=FQVw%2bGWpZgJ18g&pid=ImgRaw&r=0")));
        buttonConversion.setFill(
                new ImagePattern(new Image("https://www.shareicon.net/data/2016/06/18/596452_excel_512x512.png")));

        // Azioni svolte dai vari bottoni
        // Bottone BlackPDFLogo.png
        pdfLogo.setOnMouseClicked((event) -> {
            try {
                if (boxes.getChildren().contains(completation))
                    boxes.getChildren().remove(completation);
                fileChooser();
                boxes.getChildren().add(filesChoosed);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });

        // Bottone BlackExcelLogo.png
        buttonConversion.setOnMouseClicked((event) -> {
            try {
                conversionPDFtoEXCEL();
                completation.setFont(Font.font(STYLESHEET_CASPIAN, 15));
                if (boxes.getChildren().contains(filesChoosed))
                    boxes.getChildren().remove(filesChoosed);
                boxes.getChildren().add(completation);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });

        // Creazione elementi aggiuntivi (scritte) della scena
        Text text1 = new Text(
                "Clicca il logo PDF per scegliere i file .pdf e il logo Excel per convertirli in file .xlsx.");
        Text text2 = new Text("Li potrai visualizzare nella stessa cartella del file .pdf selezionato.");
        text1.setFont(Font.font(STYLESHEET_CASPIAN, 15));
        text2.setFont(Font.font(STYLESHEET_CASPIAN, 15));

        // Creazione della box orizzontale contenente i bottoni
        HBox buttonHb = new HBox(pdfLogo, buttonConversion);
        buttonHb.setSpacing(30);
        buttonHb.setAlignment(Pos.CENTER);

        // Configurazione della box verticale che comprende tutti gli elementi della
        // scena
        boxes = new VBox(text1, text2, buttonHb);
        boxes.setSpacing(40);
        boxes.setAlignment(Pos.CENTER);

        // Costruzione finale della scena
        Scene scene = new Scene(boxes, 620, 360);
        scene.setFill(Color.CADETBLUE);

        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * Metodo per la gestione completa (creazione, setup e funzionamento) del
     * FileChooser
     */
    public void fileChooser() {
        // Inizializzazione del FileChooser
        FileChooser fileChooser = new FileChooser();

        // Configurazione del FileChooser
        fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("PDF", "*.pdf"));
        fileChooser.setTitle("Scegli i file PDF da convertire");

        // Scelta dei file tramite FileChooser
        file = fileChooser.showOpenMultipleDialog(primaryStage);
    }

    /**
     * Metodo per la conversione del/dei file selezionato/i
     */
    public void conversionPDFtoEXCEL() {
        for (int i = 0; i < file.size(); i++) {
            // Variabile di classe File per gestire singolarmente i file della lista
            File convF = file.get(i);

            // Variabili String che contengono nomi di filePDF, fileXLSX e dirFiles
            pdfFileName = convF.getName();
            excelFileName = pdfFileName.replaceFirst(".pdf", ".xlsx");
            dir = convF.getAbsolutePath().replaceFirst(pdfFileName, "");

            // Creazione di una variabile di classe PdfDocument che conterra' il file preso
            // dalla lista
            Converter pdf = new Converter(dir + pdfFileName);

            // Caricamento del file .pdf
            SpreadsheetConvertOptions opsConv = new SpreadsheetConvertOptions();
            System.out.println("Conversione del file .pdf seguente: " + pdfFileName);

            // Salvataggio del file .pdf in file .xlsx
            pdf.convert(dir + excelFileName, opsConv);
            pdf.close();
            System.out.println("File .xlsx creato: " + excelFileName);
            System.out.println("-------------");
        }
        System.out.println("Conversione totale completata.");
    }

    /**
     * Metodo Main, il launch serve ad avviare l'intero programma
     */
    public static void main(String[] args) {
        launch(args);
    }
}

And this is my pom.xml file (Maven project)

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.conversion</groupId>
    <artifactId>application</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar </packaging>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.2.2</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>application.App</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <id>GroupDocsJavaAPI</id>
            <name>GroupDocs Java API</name>
            <url>http://repository.groupdocs.com/repo/</url>
        </repository>
    </repositories>
    <dependencies>
        <dependency>
            <groupId>com.groupdocs</groupId>
            <artifactId>groupdocs-conversion</artifactId>
            <version>22.3</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>13</version>
        </dependency>
    </dependencies>
</project>

INSTRUCTIONS OF THE PROGRAM:

  1. Press PDF Logo to load PDF files
  2. Press Excel Logo to start the conversion ("CONVERSIONE FINITA" means that it has finished).

When I run it in cmd, using the cmdline:

java -jar application-0.0.1-SNAPSHOT.jar

it gives me those lines of errors:

Exception in thread "JavaFX Application Thread" Exception in thread "main" java.lang.NoClassDefFoundError: com/groupdocs/conversion/options/convert/ConvertOptions
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Unknown Source)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplicationWithArgs$2(LauncherImpl.java:352)
        at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
        at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
        at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
        at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at com.sun.glass.ui.win.WinApplication.lambda$null$4(WinApplication.java:185)
        at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: com.groupdocs.conversion.options.convert.ConvertOptions
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 11 more
java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.NullPointerException
        at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:383)
        at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
        ... 5 more

I tried so many times to fix them but I really can't figure out a solution.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Bobbocs
  • 1
  • 2
  • 2
    Does this answer your question? [How can I create an executable JAR with dependencies using Maven?](https://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven) – Reg Jul 25 '22 at 09:53
  • I could say "yes", but I can't fgure out, for example, where i can use the line ```mvn clean assembly:single``` or others. I would prefer a specific explanation of how I can fix those, which programs i have to use and why. I know that I'm asking a lot, but it's the only thing that can help me to solve this problem. – Bobbocs Jul 25 '22 at 10:00
  • How did you create your jar file? – Mirco0 Jul 25 '22 at 10:20
  • Before now, I usually went to Export>JAR File/Executable JAR File right-clicking on te project, but it produced only an impossible-to-open jar file. Now I go to Run As> Maven Build and write "clean package" as goal. In that way it creates the file "application-0.0.1-SNAPSHOT.jar" in application\target folder. But both of them give me the same errors. – Bobbocs Jul 25 '22 at 10:29
  • Does this helps? https://stackoverflow.com/a/10019803/18648400 – Mirco0 Jul 25 '22 at 10:45
  • @Mirco0 if I write this in my pom.xml file, what I have to do after that? – Bobbocs Jul 25 '22 at 10:50
  • After that try building the jar as usual – Mirco0 Jul 25 '22 at 10:53
  • 2
    @Mirco0 it worked!!!! I was blocked there for a month and now I can finally confirm that I have finished my project! tysm – Bobbocs Jul 25 '22 at 12:05
  • @Mirco0 now I have another problem. That app has to do his job on other compuers, too. I tried to send it to 2 of my "collegues" but it seems that only one of them can run it properly. The other one says ```JavaFx runtime components are missing``` but I really don't know what to do. – Bobbocs Jul 25 '22 at 13:43
  • I'm glad I managed to help you – Mirco0 Jul 25 '22 at 14:25
  • Try adding JavaFX as Dependency as shown here https://stackoverflow.com/a/52470141/18648400 – Mirco0 Jul 25 '22 at 14:28
  • @Mirco0 thx but I figured out a solution. I already added JavaFx as Dependency (i think?) so I was on the verge to screw up. But I found a solution: I created a SuperMain.java class, that I would set as main class, with this code: ```App.main(args);``` In this way, I use a new main class that doesn't inherit Application class of JavaFx, making possible to run it without VM Arguments – Bobbocs Jul 25 '22 at 14:50
  • Yeah! Finally it worked! – Bobbocs Jul 25 '22 at 15:27

0 Answers0