Maven Error: Could not find or load main class

134,717

Solution 1

I got this error using Maven, and I discovered the solution.

Error: Could not find or load main class com.mycompany.testapifactory.Main

I'm using java JDK version 1.7 on Linux, my pom.xml file was the default generated by Netbeans and I was using these commands to compile, which do work fine with a normal hello-world java application:

mvn clean compile
java -jar target/TestAPIFactory-1.0-SNAPSHOT.jar com.mycompany.testapifactory.Main

What happened:

It turns out my problem was that my Main method was extending something Exotic like this:

public class Main extends SomeExoticLibraryClass{
    public static void main(String[] args){
        //...
    }
}

It was this extending of the main class that caused the above error.

TLDR solution:

Make sure your main class isn't extending any 3rd party classes. Refactor those out and away into their own classes. That error message is awful, and requires process of elimination to find out what to do.

Solution 2

Unless you need the 'maven-assembly-plugin' for reasons other than setting the mainClass, you could use the 'maven-jar-plugin' plugin.

     <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <index>true</index>
                    <manifest>
                        <mainClass>your.package.yourprogram.YourMainClass</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
     </plugins>

You can see the plugin in practise in the ATLauncher.

The 'mainClass' element should be set to the class that you have the entry point to your program in eg:

package your.package.yourprogram;

public class YourMainClass {

    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}

Solution 3

Please follow the below snippet.. it works..

<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>com.xyz</groupId>
    <artifactId>test</artifactId>
    <packaging>jar</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>TestProject</name>
    <description>Sample Project</description>
    <dependencies>
        <!-- mention your dependencies here -->
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.1.1</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.xyz.ABC.</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Please note, you have to provide the full classified class name (class name including package name without .java or .class) of main class inside <mainClass></mainClass> tag.

Solution 4

For me the problem was nothing to do with Maven but to do with how I was running the .jar. I wrote some code and packaged it as a .jar with Maven. I ran it with

java target/gs-maven-0.1.0.jar

and got the error in the OP. Actually you need the -jar option:

java -jar target/gs-maven-0.1.0.jar

Solution 5

I got it too, for me the problem got resolved after deleting the m2 folder (C:\Users\username.m2) and updating the maven project.

Share:
134,717

Related videos on Youtube

MLMLTL
Author by

MLMLTL

Please Help Me :'(

Updated on July 09, 2022

Comments

  • MLMLTL
    MLMLTL almost 2 years

    I'm using a Java Maven program and I don't know what to enter as the <mainClass>. I've tried all kinds of things based off of numerous stackoverflow questions, but they are not solving the error.

    Each time it says:

    Maven Error: Could not find or load main class ...
    

    I have this written inside my pom.xml (minus the ???)

      <build>
      ...
      <plugins>
      ...
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.5.3</version>
            <configuration>
                <descriptors>
                    <descriptor>src/main/assembly/jar-with-dependencies.xml</descriptor>
                </descriptors>
                <archive>
                    <manifest>
                        <mainClass> ??? </mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                <id>make-assembly</id>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
                </execution>
            </executions>
        </plugin>
      ...
      </plugins>
      ...
      </build>
    

    How do I fix these errors?

    • khmarbaise
      khmarbaise about 9 years
      Please show your full pom file.
  • Chandra Shekhar
    Chandra Shekhar over 6 years
    perfect solution !
  • Michiel
    Michiel about 6 years
    I had this issue with my JavaFX project that extended JavaFX's Application class. While removing the extension "fixed" the issue, it seems that using Oracle's JDK for running the application instead of the OpenJDK that came with my Linux distro fixed it without requiring me to change the code. It might have something to do with the extended class not being available/loaded at launch?
  • Danushka herath
    Danushka herath about 4 years
    Saved my whole day! Thanks @Eric
  • user1445967
    user1445967 almost 4 years
    Thank you. Sad (for our technology ecosystem) that I had to read dozens of answers to find this.
  • Poli
    Poli over 2 years
    This should fix Could not find or load main class error for everyone