问题描述
我已经尝试了几天,为我的多模块maven项目创建可执行的jar文件.但是,当我尝试运行此jar文件时,出现找不到或加载主类src.main.java.com.domain.Mainclass"(出于公司隐私的考虑,我更改了名称domain和MainClass)
I have been trying for several days now to create an executable jar file for my muli-module maven project. However, when I try to run this jar file I get "Could not find or load main class src.main.java.com.domain.Mainclass" (I have changed the name domain and MainClass for my company's privacy sake)
我已经搜索了几天,似乎没有任何明显的提示有用.
I have searched for days and none of the obvious tips seem to work.
我的项目有一个主要的maven项目,该项目下载所有依赖项(packaging:POM)和几个模块项目(包装:Jar).
My project has a main maven project that downloads all of the dependencies (packaging:POM)and several module projects (Packaging:Jar).
一切似乎都可以正常工作,所有文件都被编译成类文件,但是不知何故没有将主类添加到pom中.
Everything seems to work fine, and all of the files are compiled into class files, but somehow the main class is not being added to the pom.
我的Pom文件插件:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-4</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.domain.project.MainClass</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
我使用的命令:mvn clean package,然后我进入目标文件夹并执行:java -jar jarfilename.jar
The Commands I Use: mvn clean package, then I cd into the target folder and do: java -jar jarfilename.jar
任何提示或帮助都将受到欢迎!
Any tips or help would be most welcomed!
我当前的配置为每个模块创建2个jar文件:
My current configuration creates a 2 jar files for every module:
- projectname-jar-with-dependencies.jar
- projectname.jar
当我使用主类导航到模块的目标文件夹时,可以成功运行jar文件.但是,当我尝试运行我的父Maven项目(带有packageing:pom的那个项目)的jar文件时,仍然出现相同的错误.有谁知道为什么主jar文件找不到主类?
When I navigate to the target folder of the module with my main class, I can successfully run the jar file. However, when I try to run the jar file of my parent maven project (the one with packageing:pom) I still get the same error. Does anyone know why the main jar file cannot find the main class?
谢谢!
推荐答案
您不应将src.main.java
作为主类的包名称的一部分.它只是默认的Maven项目文件夹结构约定的一部分.您的配置可能应该显示为:
You should not have src.main.java
as part of the package name of your main class. It's just part of the default maven project folder structure convention. Your configuration should probably read:
<archive>
<manifest>
<mainClass>com.domain.project.MainClass</mainClass>
</manifest>
</archive>
这篇关于Maven Jar Builder:找不到或加载主类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!