运行 java -jar 时无法加载主类清单属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19620342/
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
Failed to load Main-Class manifest attribute while running java -jar
提问by Simone
I have successfully built my Spring MVC project with mvn clean package
by following this tutorial.
我已经mvn clean package
按照本教程成功构建了我的 Spring MVC 项目。
Now I am trying to run the service with:
现在我正在尝试使用以下方式运行服务:
mvn clean package && java -jar target/gs-serving-web-content-0.1.0.jar
But I get this error:
但我收到此错误:
Failed to load Main-Class manifest attribute from target/gs-serving-web-content-0.1.0.jar
无法从 target/gs-serving-web-content-0.1.0.jar 加载 Main-Class 清单属性
Am I missing something?
我错过了什么吗?
采纳答案by Mradul Pandey
If you are working with Spring Boot this will solve your problem:
如果您正在使用 Spring Boot,这将解决您的问题:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.2.5.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
回答by Simone
You are missing maven-jar-plugin
in which you need to add manifest
tag.
您缺少maven-jar-plugin
需要添加manifest
标签的地方。
回答by Martin Seeler
You have to specifiy it in your pom.xml - This will make your jar executable with all dependencies (replace your.main.class
):
您必须在 pom.xml 中指定它 - 这将使您的 jar 可执行文件具有所有依赖项(替换your.main.class
):
<!-- setup jar manifest to executable with dependencies -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>your.main.class</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
回答by Atish Narlawar
You might be missing Spring Boot Maven Plugin.
您可能缺少 Spring Boot Maven 插件。
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
回答by Dror
For spring boot, I've created a MANIFEST.MFfile inside META-INFfolder.
对于 spring boot,我在META-INF文件夹中创建了一个MANIFEST.MF文件。
in my STS IDE, I placed the META-INFO folder inside the src/main/resourcesfolder like so:
在我的 STS IDE 中,我将 META-INFO 文件夹放在src/main/resources文件夹中,如下所示:
the content of the MANIFEST.MF file:
MANIFEST.MF 文件的内容:
Manifest-Version: 1.0
Implementation-Title: bankim
Implementation-Version: 1.5.6.RELEASE
Archiver-Version: Plexus Archiver
Built-By: Yourname
Implementation-Vendor-Id: com.bankim
Spring-Boot-Version: 1.5.6.RELEASE
Implementation-Vendor: Pivotal Software, Inc.
Main-Class: org.springframework.boot.loader.JarLauncher
Start-Class: com.bankim.BankimApplication
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_131
Implementation-URL: http://projects.spring.io/spring-boot/bankim/
- Every mention of "bankim"/"Bankim" refers to my project name, so replace it with your project name that
- take special note of the "Start-Class" value. it should contain the "path" to the class that has your mainmethod.
- the row: Main-Class: org.springframework.boot.loader.JarLaunchershould be left as-is.
- 每次提到“bankim”/“Bankim”都是指我的项目名称,因此将其替换为您的项目名称
- 特别注意“Start-Class”值。它应该包含具有您的主要方法的类的“路径” 。
- 行: Main-Class: org.springframework.boot.loader.JarLaunchers 应该保持原样。
****the above manifest was created for me by using the "spring-boot-maven-plugin" mentioned above by "Mradul Pandey" (answered Sep 2 '15 at 4:50)
****上述清单是使用“Mradul Pandey”上面提到的“spring-boot-maven-plugin”为我创建的(2015 年 9 月 2 日 4:50 回答)
Hope this helps
希望这可以帮助
回答by Musab Qamri
check the order of
检查顺序
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
it should be above
它应该在上面
dockerfile-maven-plugin else repackage will happen
dockerfile-maven-plugin 否则会发生重新打包
this solved my problem of no main attribute in manifest.
这解决了我在清单中没有主要属性的问题。
回答by veer7
I had the spring-boot-maven-plugin
but still I was getting the error regarding main class.
我有,spring-boot-maven-plugin
但仍然收到有关主类的错误。
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
Finally I had to use the maven-jar-plugin
and add the mainClass
最后我不得不使用maven-jar-plugin
并添加mainClass
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<mainClass>com.org.proj.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
And it was good to go!
很高兴去!
回答by tycoon
Use spring-boot-maven-plugin
用 spring-boot-maven-plugin
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
and be sure you set property start-class
in the pom.xml
并确保您property start-class
在 pom.xml 中设置
<properties>
<!-- The main class to start by executing "java -jar" -->
<start-class>org.example.DemoApplication</start-class>
</properties>
Alternatively, the main class can be defined as the mainClass element of the spring-boot-maven-plugin in the plugin section of your pom.xml:
或者,主类可以定义为 pom.xml 的 plugin 部分中 spring-boot-maven-plugin 的 mainClass 元素:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>spring-boot</classifier>
<mainClass>${start-class}</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
回答by Gaurav
java -jar target/gs-serving-web-content-0.1.0.jar
command assumes the provided jar is executable. In an executablejar, the main
method is defined in MANIFEST.MF
file. Given that you don't have a MANIFEST.MF
file, an additional command-line argument can be passed to specify the class containing the main
method.
java -jar target/gs-serving-web-content-0.1.0.jar
命令假定提供的 jar 是可执行的。在可执行jar 中,main
方法在MANIFEST.MF
文件中定义。鉴于您没有MANIFEST.MF
文件,可以传递一个额外的命令行参数来指定包含该main
方法的类。
A command like java -cp target/gs-serving-web-content-0.1.0.jar com.pkg.subpkg.MainClass
would serve the purpose.
类似的命令java -cp target/gs-serving-web-content-0.1.0.jar com.pkg.subpkg.MainClass
将达到目的。