且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Maven 错误:无法找到或加载主类

更新时间:2022-05-14 02:35:53

我在使用 Maven 时遇到此错误,然后我找到了解决方案.

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

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

我在 Linux 上使用 Java JDK 1.7 版,我的 pom.xml 文件是 Netbeans 生成的默认文件,我使用这些命令进行编译,它在普通的 hello-世界java应用:

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

发生了什么:

事实证明我的问题是我的 Main 方法正在扩展这样的 Exotic :

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 解决方案:

确保您的主类没有扩展任何 3rd 方类.将它们重构为它们自己的类.该错误消息很糟糕,需要消除过程才能找出要做什么.

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.