Home > Mobile >  "Could not find or load main class" in Maven jar
"Could not find or load main class" in Maven jar

Time:09-22

I know this question has been asked a lot but non of the answers worked for me.

I keep getting "Error: Could not find or load main class app.Main.java" when trying to run my jar (created with maven).

CMD command:

java -jar .\RideShare-1.0-SNAPSHOT.jar

pom.xml :

<?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>org.example</groupId>
    <artifactId>RideShare</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
<!--                            <classpathPrefix>libs/</classpathPrefix>-->
                            <mainClass>app.Main.java</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>


    <dependencies>
        .
        .
        .
    </dependencies>


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

</project>

and this is a screenshot of my project's structure:
enter image description here

What am I missing?

CodePudding user response:

as @slindenau mentioned in his comment, the problem was that I used the wrong class name format in <mainClass>...</mainClass>.

To get the fully qualified name (using IntelliJ), I did:
right-click on the class name -> "Copy Path/Reference" -> "Path From Source Root".
and that gave me "app/Main.java" while what I needed was "app/Main".

So finally,

<mainClass>app.Main</mainClass>

worked for me.

  • Related