Java Maven 错误:无法找到或加载主类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29366373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Maven Error: Could not find or load main class
提问by MLMLTL
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 numerousstackoverflow questions, but they are not solving the error.
我正在使用 Java Maven 程序,但我不知道要输入什么作为<mainClass>
. 我已经尝试了基于大量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?
如何修复这些错误?
回答by khmarbaise
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。
回答by jamierocks
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");
}
}
回答by Eric Leschinski
I got this errorusing 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. 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.
确保您的主类没有扩展任何 3rd 方类。将它们重构为它们自己的类。该错误消息很糟糕,需要消除过程才能找出要做什么。
回答by WesternGun
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
.
我得到了它太多,关键是从更改输出文件夹bin
到target\classes
。似乎在Eclipse中,将项目转换为Maven项目时,这一步不是自动完成的,但是Maven项目不会基于 寻找主类bin
,而是会在target\classes
.
回答by Vishakh Rameshan
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>
回答by Hyman M
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
对我来说,问题与 Maven 无关,而是与我运行 .jar 的方式有关。我编写了一些代码并使用 Maven 将其打包为 .jar。我运行它
java target/gs-maven-0.1.0.jar
and got the error in the OP. Actually you need the -jar
option:
并在 OP 中得到错误。其实你需要这样的-jar
选择:
java -jar target/gs-maven-0.1.0.jar
回答by Prabhat Kumar Singh
I got it too, for me the problem got resolved after deleting the m2 folder (C:\Users\username.m2) and updating the maven project.
我也明白了,对我来说,在删除 m2 文件夹(C:\Users\username.m2)并更新 maven 项目后问题得到了解决。
回答by Abhishek Gupta
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. And instead of your "???" write ${mainClass}which will automatically get the className which you have mentioned above.
并添加项目的类名(完整路径)以及包名,如“com.packageName.className”,其中包含包含“run”方法的 main 方法。而不是你的“???” 编写${mainClass}它将自动获取您上面提到的 className。
Then try command mvn clean installand mvn -jar "jar_file_name.jar" server "yaml_file_name.yml"
然后尝试命令mvn clean install和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.
我希望它可以正常工作并且服务器将在指定的端口启动。
回答by Priya
I got this error(classNotFoundException for main class), I actually changed pom version , so did maven install again and then error vanished.
我收到此错误(主类的 classNotFoundException),我实际上更改了 pom 版本,因此再次安装了 maven,然后错误消失了。
回答by Sumit Karmakar
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.
请注意,您必须在标签内提供主类的完整分类类名(类名包括包名,没有.java
或.class
)<mainClass></mainClass>
。