简体   繁体   中英

Maven Error: Could not find or load main class

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?

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.

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");
    }
}

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.

我也明白了,对我来说,在删除 m2 文件夹(C:\\Users\\username.m2)并更新 maven 项目后问题得到了解决。

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 .

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

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>

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.

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.

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 .

我收到此错误(主类的 classNotFoundException),我实际上更改了 pom 版本,因此再次安装了 maven,然后错误消失了。

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

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>

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> -->

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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM