运行 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:51:48  来源:igfitidea点击:

Failed to load Main-Class manifest attribute while running java -jar

javaspringmavenspring-mvc

提问by Simone

I have successfully built my Spring MVC project with mvn clean packageby 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>

Reference Guide | Spring Boot Maven Plugin

参考指南 | Spring Boot Maven 插件

回答by Simone

You are missing maven-jar-pluginin which you need to add manifesttag.

您缺少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文件夹中,如下所示:

screenshot from STS IDE (eclipse project)

STS IDE 截图(eclipse 项目)

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/
  1. Every mention of "bankim"/"Bankim" refers to my project name, so replace it with your project name that
  2. take special note of the "Start-Class" value. it should contain the "path" to the class that has your mainmethod.
  3. the row: Main-Class: org.springframework.boot.loader.JarLaunchershould be left as-is.
  1. 每次提到“bankim”/“Bankim”都是指我的项目名称,因此将其替换为您的项目名称
  2. 特别注意“Start-Class”值。它应该包含具有您的主要方法的类的“路径” 。
  3. 行: 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-pluginbut 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-pluginand 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-classin 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.jarcommand assumes the provided jar is executable. In an executablejar, the mainmethod is defined in MANIFEST.MFfile. Given that you don't have a MANIFEST.MFfile, an additional command-line argument can be passed to specify the class containing the mainmethod.

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.MainClasswould serve the purpose.

类似的命令java -cp target/gs-serving-web-content-0.1.0.jar com.pkg.subpkg.MainClass将达到目的。