Maven Error: Could not find or load main class

匿名 (未验证) 提交于 2019-12-03 02:20:02

问题:

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?

回答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.



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


回答3:

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.



回答4:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!