Maven java.lang.ClassNotFoundException: Could not find or load main class

56 views Asked by At

I have a problem with my Maven project. The program was already running without any problem but now I can't start it anymore. It is important that the user can run this application by just double click it and not use the command "java -jar ...". I have updated my project from Java 16 to Java 21 and have customized some fxml files. The fxml files should not cause any problems as these are purely visual adjustments.

Unfortunately, I'm not sure about the rest. So here is a short description of the problem.

  1. If I run the program directly in Intellij, there are no problems and the program starts.

  2. If I create my .jar via maven package, which has already worked in the past. Here are two different behaviors.

a. If I start the .jar via the command line "java -jar .\vertragsmanager-1.0-SNAPSHOT.jar" everything works fine. The application starts as in Intellij.

b. If I run the .jar also in the command line with the command "java .\vertragsmanager-1.0-SNAPSHOT.jar" I receive the error "java.lang.ClassNotFoundException: Could not find or load main class"

I suspect that my pom.xml has a problem. Therefore I have attached it here. As you guys can imagine - the main class is GUIstarter.class.

<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>de.XXX</groupId>
    <artifactId>vertragsmanager</artifactId>

    <version>1.0-SNAPSHOT</version>

    <repositories>
        <repository>
            <id>com.e-iceblue</id>
            <name>e-iceblue</name>
            <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
        </repository>
    </repositories>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.32</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version>
        </dependency>

        <dependency>
            <groupId>com.jcraft</groupId>
            <artifactId>jsch</artifactId>
            <version>0.1.55</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.6.9.Final</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.30</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-media</artifactId>
            <version>21</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-swing</artifactId>
            <version>21</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>21</version>
        </dependency>

        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>21</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-graphics</artifactId>
            <version>21</version>
            <classifier>win</classifier>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-graphics</artifactId>
            <version>21</version>
            <classifier>linux</classifier>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-graphics</artifactId>
            <version>21</version>
            <classifier>mac</classifier>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>2.0.5</version>
        </dependency>

        <dependency>
            <groupId>e-iceblue</groupId>
            <artifactId>spire.doc</artifactId>
            <version>12.2.2</version>
        </dependency>


        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>5.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>5.2.2</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.openjfx/javafx-web -->
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-web</artifactId>
            <version>17.0.2</version>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.5.0</version>

                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <manifestEntries>
                                        <Main-Class>de.XXX.vertragsmanager.bauvertrag.GUIStarter</Main-Class>
                                        <Build-Number>1.0.0</Build-Number>
                                        <SplashScreen-Image>images/SplashScreen.png</SplashScreen-Image>
                                    </manifestEntries>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>


    <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
    </properties>

</project>

This is the exact error (it is in german - sorry for that):

Fehler: Hauptklasse .\vertragsmanager-1.0-SNAPSHOT.jar konnte nicht gefunden oder geladen werden
Ursache: java.lang.ClassNotFoundException: /\vertragsmanager-1/0-SNAPSHOT/jar

Thank you very much in advance!

I have already tried to clean the maven project and repackage it. I also checked many other questions on stack overflow but none of these answers could fix my issue.

2

There are 2 answers

0
Stephen C On

If I run the .jar also in the command line with the command java .\vertragsmanager-1.0-SNAPSHOT.jar I receive the error

"java.lang.ClassNotFoundException: Could not find or load main class"

You are just running your app the wrong way. That's not how you run an executable JAR file. The alternatives are:

java -jar path-to-jar-file

or

java -cp path-to-jar-file full-class-name

In your case, the latter form would be:

java -cp  .\vertragsmanager-1.0-SNAPSHOT.jar \
          de.XXX.vertragsmanager.bauvertrag.GUIStarter

(But why do it that way if you have already gone to the trouble of creating an executable JAR file?)

For more information, please refer to What does "Could not find or load main class" mean?


I am wondering why java .\vertragsmanager-1.0-SNAPSHOT.jar was working before I changed from Java 16 to Java 21

I would only believe that if I saw it with my own eyes. It should not work with any version of Java. The only plausible explanation for it working is that someone has done something funky ... like defining a shell alias for the java command.

0
Odosco On

The answer was quite simple. I found out that Java 16 was still entered in the registry for .jar files. When I changed the path here to Java 21, I no longer got the error and was able to open the .jar.

I'm just surprised that the error message doesn't really point this out, but says that the class doesn't exist.

But thank you all for your help anyway!!!