Maven Error: Could not find or load main class
Asked Answered
M

16

52

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?

Mazur answered 31/3, 2015 at 10:24 Comment(3)
Please show your full pom file.Delphadelphi
I'm guessing you don't have a main class, because you're packing this as a jar library, but I can't tell for sure from your question or your .pom fragment. I'm having the same problem, and some of the answers below are explaining how to specify the main class.Bruges
Pavel Grigorev's answer to another question fixed this issue for me. In my case, I didn't have a main class because I was just building a jar file. This was in a Spring Boot project. You can find Grigorev's answer at #42938077Bruges
M
33

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.

Muro answered 30/6, 2016 at 21:59 Comment(2)
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?Chorea
The trouble with this answer is that I don't have a main class. I'm building a library. Why is it even looking for a main class?Bruges
P
27

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");
    }
}
Polyvalent answered 5/4, 2015 at 20:43 Comment(1)
The trouble with this answer is that I don't have a main class. I'm building a library. Why is it even looking for a main class?Bruges
A
11

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
Agog answered 29/11, 2018 at 8:35 Comment(2)
This should fix Could not find or load main class error for everyoneTrunks
The question was about a maven error. This fixes a different error with the same message.Bruges
V
7

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.

Vittoria answered 6/6, 2019 at 10:8 Comment(0)
F
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.

Felafel answered 5/1, 2019 at 15:50 Comment(0)
T
3

I got it too, the key was to change the output folder from bin to target\classes. It seems that in Eclipse, when converting a project to Maven project, this step is not done automatically, but Maven project will not look for main class based on bin, but will on target\classes.

Tide answered 12/9, 2017 at 16:53 Comment(0)
L
2

specify the main class location in pom under plugins

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <index>true</index>
                        <manifest>
                            <mainClass>com.example.hadoop.wordCount.WordCountApp</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
Lubricate answered 27/9, 2018 at 14:40 Comment(0)
I
2

add this to your pom.xml file:

<configuration>
   <transformers>
       <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
       <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
         <mainClass>sample.HelloWorldApplication</mainClass>
       </transformer>
   </transformers>
</configuration>

and add the class name of your project (full path) along with the package name like "com.packageName.className" which consists of the main method having "run" method in it. And instead of your "???" write ${mainClass} which will automatically get the className which you have mentioned above.

Then try command mvn clean install and mvn -jar "jar_file_name.jar" server "yaml_file_name.yml"

I hope it will work normally and server will start at the specified port.

Ibbetson answered 8/1, 2019 at 20:52 Comment(0)
D
1

The first thing i would suggest is to use the correct configuration for predefined descriptors.

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.5.3</version>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
        [...]
</project>

To configure the main class you need to know the package and name of the class you would like to use which should be given into <mainClass>...</mainClass> parameter.

Furthermore i recommend to stop using Maven 2 and move to Maven 3 instead.

Delphadelphi answered 2/4, 2015 at 8:7 Comment(0)
E
1

I got this error(classNotFoundException for main class), I actually changed pom version , so did maven install again and then error vanished.

Era answered 27/3, 2019 at 11:2 Comment(0)
J
1

TLDR : check if packaging element inside the pom.xml file is set to jar.

Like this - <packaging>jar</packaging>. If it set to pom your target folder will not be created even after you Clean and Build your project and Maven executable won't be able to find .class files (because they don't exist), after which you get Error: Could not find or load main class your.package.name.MainClass


After creating a Maven POM project in Netbeans 8.2, the content of the default pom.xml file are as follows -

<?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>com.mycompany</groupId>
   <artifactId>myproject</artifactId>
   <version>1.0-SNAPSHOT</version>
   <packaging>pom</packaging>
   <properties>
       <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   </properties>
</project>

Here packaging element is set to pom. Hence the target directory is not created as we are not enabling maven to package our application as a jar file. Change it to jar then Clean and Build your project, you should see target directory created at root location. Now you should be able to run that java file with main method.

When no packaging is declared, Maven assumes the packaging as jar. Other core packaging values are pom, war, maven-plugin, ejb, ear, rar. These define the goals that execute on each corresponsding build life-cycle phase of that package. See more here

Joris answered 17/5, 2020 at 8:39 Comment(0)
M
1

this worked for me....

I added the following line to properties in pom.xml

<properties>
    <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
</properties>
Marilla answered 30/8, 2020 at 2:38 Comment(0)
S
1

For me, I added

<packaging>jar</packaging>

and removed the default spring-boot-maven-plugin

<!-- remove this plugin
    <plugin> 
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin> -->
Stapes answered 4/3, 2021 at 2:41 Comment(0)
P
1

Add the -Xdiag option at the execution. This is extra "diagnostic". This will not solve the issue but add more detailed error messages and root causes that help identifying the issue.

Pixilated answered 23/3, 2021 at 21:56 Comment(0)
K
1

In a rare occasion, an emoji in the access path caused this type of error. Double check your directory names, maybe a non standard character makes all the fun!

Karinkarina answered 19/4, 2021 at 9:1 Comment(0)
P
1

I am late to the party in my case it was the image I was using that was causing the trouble

Working config

<plugin>
            <groupId>com.google.cloud.tools</groupId>
            <artifactId>jib-maven-plugin</artifactId>
            <version>${jib-maven-plugin.version}</version>
            <configuration>
                <from>
                    <image>openjdk:11-jre-slim</image>
                </from>
                <to>
                    <image>${docker.image.prefix}/${project.artifactId}</image>
                </to>
                <container>
                    <mainClass>com.example.configserverApplication</mainClass>
                    <ports>
                        <port>8888</port>
                    </ports>
                </container>
            </configuration>
        </plugin>

Failing one

  <plugin>
            <groupId>com.google.cloud.tools</groupId>
            <artifactId>jib-maven-plugin</artifactId>
            <version>${jib-maven-plugin.version}</version>
            <configuration>
                <from>
                    <image>openjdk:alpine</image>
                </from>
                <to>
                    <image>${docker.image.prefix}/${project.artifactId}</image>
                </to>
                <container>
                    <mainClass>com.example.configserverApplication</mainClass>
                    <ports>
                        <port>8888</port>
                    </ports>
                </container>
            </configuration>
        </plugin>

My understanding is that

"The OpenJDK port for Alpine is not in a supported release by OpenJDK, since it is not in the mainline codebase. It is only available as early access builds of OpenJDK Project Portola. See also this comment. So this image follows what is available from the OpenJDK project's maintainers."

https://hub.docker.com/_/openjdk

it is weird that it fails with a one-line error and changing the image fixed it for me.

Paly answered 29/9, 2021 at 18:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.