简体   繁体   English

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

[英]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> .我正在使用 Java Maven 程序,但我不知道要输入什么作为<mainClass> I've tried all kinds of things based off of numerous stackoverflow questions , but they are not solving the error.我已经根据众多stackoverflow 问题尝试了各种方法,但它们并没有解决错误。

Each time it says:每次它说:

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

I have this written inside my pom.xml (minus the ??? )我把这个写在我的pom.xml (减去???

  <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.我在使用 Maven 时遇到了这个错误,我发现了解决方案。

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:我在 Linux 上使用 java JDK 1.7 版,我的pom.xml文件是 Netbeans 生成的默认文件,我使用这些命令进行编译,它在普通的 hello-world java 应用程序中运行良好:

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:原来我的问题是我的 Main 方法正在扩展这样的 Exotic 东西:

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

Make sure your main class isn't extending any 3rd party classes.确保您的主类没有扩展任何 3rd 方类。 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.除非您出于设置 mainClass 以外的原因需要“maven-assembly-plugin”,否则您可以使用“ maven-jar-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 .您可以在ATLauncher 中看到该插件的实际应用

The 'mainClass' element should be set to the class that you have the entry point to your program in eg: 'mainClass' 元素应设置为您的程序入口点所在的类,例如:

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.请注意,您必须在<mainClass></mainClass>标签内提供主类的完整分类类名(类名包括包名,不带.java.class )。

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

I got it too, the key was to change the output folder from bin to target\\classes .我也明白了,关键是将输出文件夹从bin更改为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 .似乎在 Eclipse 中,将项目转换为 Maven 项目时,这一步不会自动完成,但是 Maven 项目不会基于bin查找主类,而是会在target\\classes

For me the problem was nothing to do with Maven but to do with how I was running the .jar.对我来说,问题与 Maven 无关,而是与我运行 .jar 的方式有关。 I wrote some code and packaged it as a .jar with Maven.我编写了一些代码并使用 Maven 将其打包为 .jar。 I ran it with我运行它

java target/gs-maven-0.1.0.jar

and got the error in the OP.并在 OP 中得到错误。 Actually you need the -jar option:实际上你需要-jar选项:

java -jar target/gs-maven-0.1.0.jar

specify the main class location in pom under plugins在插件下的pom中指定主类位置

<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:将此添加到您的 pom.xml 文件中:

<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.并添加项目的类名(完整路径)以及包名,如“com.packageName.className”,其中包含包含“run”方法的 main 方法。 And instead of your "???"而不是你的“???” write ${mainClass} which will automatically get the className which you have mentioned above.编写${mainClass}它将自动获取您上面提到的 className。

Then try command mvn clean install and mvn -jar "jar_file_name.jar" server "yaml_file_name.yml"然后尝试命令mvn clean installmvn -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." “Alpine 的 OpenJDK 端口不在 OpenJDK 支持的版本中,因为它不在主线代码库中。它仅可作为 OpenJDK Project Portola 的早期访问版本使用。另请参阅此评论。所以这张图片遵循可从OpenJDK 项目的维护者。”

https://hub.docker.com/_/openjdk 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.要配置主类,您需要知道要使用的类的包和名称,应将其指定给<mainClass>...</mainClass>参数。

Furthermore i recommend to stop using Maven 2 and move to Maven 3 instead .此外,我建议停止使用 Maven 2 并改为使用 Maven 3

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

TLDR : check if packaging element inside the pom.xml file is set to jar . TLDR:检查 pom.xml 文件中的packaging元素是否设置为jar

Like this - <packaging>jar</packaging> .像这样 - <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如果它设置为pom即使在您清理并构建您的项目之后,您的目标文件夹也不会被创建,并且 Maven 可执行文件将无法找到.class文件(因为它们不存在),之后您会收到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 -在Netbeans 8.2中创建Maven POM项目后,默认的pom.xml文件内容如下——

<?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 .这里的packaging元素设置为pom Hence the target directory is not created as we are not enabling maven to package our application as a jar file.因此不会创建target目录,因为我们没有启用 maven 将我们的应用程序打包为jar文件。 Change it to jar then Clean and Build your project, you should see target directory created at root location.将其更改为jar然后清理并构建您的项目,您应该会看到在根位置创建的目标目录。 Now you should be able to run that java file with main method.现在您应该能够使用 main 方法运行该 java 文件。

When no packaging is declared, Maven assumes the packaging as jar .当没有声明包装时,Maven 假定包装为jar Other core packaging values are pom , war , maven-plugin , ejb , ear , rar .其他核心封装值是pomwarmaven-pluginejbearrar 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我将以下行添加到 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并删除了默认的 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.在执行时添加-Xdiag选项。 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!仔细检查您的目录名称,也许一个非标准字符会让一切变得有趣!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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