Maven 错误:无法找到或加载主类 [英] Maven Error: Could not find or load main class

查看:33
本文介绍了Maven 错误:无法找到或加载主类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Java Maven 程序,但我不知道要输入什么作为 .我已经尝试了基于 无数 的各种事情stackoverflow 问题,但他们没有解决错误.

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.

每次都说:

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

我在我的 pom.xml 中写了这个(减去 ???)

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>

如何修复这些错误?

推荐答案

我在使用 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.

这篇关于Maven 错误:无法找到或加载主类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆