错误:无法在 Java Maven 项目中找到或加载主类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23951110/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 09:20:17  来源:igfitidea点击:

Error: Could not find or load main class in Java Maven project

javamavenpom.xml

提问by Anand

I am working in Java Maven project. There is a .batfile in the root of the project which invokes a Java class with some arguments something like this:

我在 Java Maven 项目中工作。.bat项目的根目录中有一个文件,它调用一个带有一些参数的 Java 类,如下所示:

java my.package.MyClass abc hi 1

Now, my project jar is built in the target directory of that project when i do mvn clean install. When I run that .batfile it gives me the below error

现在,我的项目 jar 构建在该项目的目标目录中mvn clean install。当我运行该.bat文件时,它给了我以下错误

Error: Could not find or load main class my.package.MyClass

Project's pom.xmlonly contains jars as dependency.

项目pom.xml仅包含 jars 作为依赖项。

Can someone please help me with this.Do i need to do something in pom.xmlto make it work.

有人可以帮我解决这个问题吗?我需要做些什么pom.xml才能让它发挥作用。

回答by reno

please provide your pom.xmlso we can look for it, anyway,

请提供您的,pom.xml以便我们可以寻找它,无论如何,

do you use maven-jar-plugin?

你用maven-jar-plugin吗?

something like this

像这样的东西

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-jar-plugin</artifactId>
   <version>2.4</version>
   <configuration>
     <archive>
       <manifest>
         <addClasspath>true</addClasspath>
         <classpathPrefix>lib/</classpathPrefix>
         <mainClass>your.main.class.package.ClassName</mainClass> // your main class
       </manifest>
     </archive>
   </configuration>
 </plugin>

and try to run your *.jarwith command java -jar yourjar.jar

并尝试运行你*.jar的命令java -jar yourjar.jar