运行java jar - 没有主清单属性错误

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

Run java jar - no main manifest attribute error

javamavenintellij-ideapom.xml

提问by Jenny Hilton

I've created simple java program (maven with pom ) which when I run some command with CMD it should created a file under given path... I do mvn clean installwhich finish successfully, Now I want to use this created jar from the command line like follwoing:

我创建了简单的 java 程序(带有 pom 的 maven),当我用 CMD 运行一些命令时,它应该在给定的路径下创建一个文件......我mvn clean install成功完成了,现在我想从命令行使用这个创建的 jar以下:

java -jar   "/Users/i012/IdeaProjects/myproj/target/test.rts-1.0-SNAPSHOT.jar" path2genfile2create 

Which should run my program (this the first time that I try something like this…)

哪个应该运行我的程序(这是我第一次尝试这样的事情......)

But the error which Im getting is:

但我得到的错误是:

no main manifest attribute, in /Users/i012/IdeaProjects/myproj/target/test.rts-1.0-SNAPSHOT.jar

What could be missing here ? which manifest attribute?

这里可能缺少什么?哪个清单属性

The error is not coming from the class i've created

错误不是来自我创建的类......

i've created some META-INF/MANIFEST.MF not helping but maybe Its wrong

我创建了一些 META-INF/MANIFEST.MF 没有帮助,但可能是错误的

采纳答案by Kevin Boone

If you're using the Maven assembly plug-in, or your IDE tooling is, you need a mainClasselement. This is what I use:

如果您使用的是 Maven 程序集插件,或者您的 IDE 工具,则需要一个mainClass元素。这是我使用的:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
      <archive>
        <manifest>
          <mainClass>com.foo.MyMainClass</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>

回答by alirabiee

A manifest is a file in the path META-INF/MANIFEST.MF within the jar which defines attributes like the classpath and the main class for running the jar file.

清单是 jar 中 META-INF/MANIFEST.MF 路径中的一个文件,它定义了类路径和用于运行 jar 文件的主类等属性。

The basic structure would be like:

基本结构如下:

Manifest-Version: 1.0
Created-By: 1.7.0_06 (Oracle Corporation)

You can define your entry point by adding the property Main-Class: classname.

您可以通过添加属性来定义您的入口点Main-Class: classname

In order to create your jar file with a given manifest you can:

为了使用给定的清单创建 jar 文件,您可以:

  1. Use your IDE to add a manifest to the jar it generates.
  2. Use a command like jar cfm MyJar.jar Manifest.txt MyPackage/*.classto manually create a jar with the given manifest and classes.
  3. Manually decompress the jar, add the manifest, and compress it again. Compression tools generally could do this with a drag/drop.
  1. 使用您的 IDE 将清单添加到它生成的 jar 中。
  2. 使用类似命令jar cfm MyJar.jar Manifest.txt MyPackage/*.class手动创建具有给定清单和类的 jar。
  3. 手动解压jar,添加manifest,再次压缩。压缩工具通常可以通过拖放来做到这一点。

You can find out more about the jar manifest file here.

您可以在此处找到有关 jar 清单文件的更多信息。

回答by saman

in my case, I was using spring-boot but i did not mentioned my builder in my pom so i fixed it by:

就我而言,我使用的是 spring-boot,但我没有在我的 pom 中提到我的构建器,所以我通过以下方式修复它:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

回答by Dulith De Costa

You need a main class to execute you application. Try the following. It worked for me.

您需要一个主类来执行您的应用程序。请尝试以下操作。它对我有用。

Add the following code snippet to your pom.xml file if you are using maven build tool.

如果您使用的是 maven 构建工具,请将以下代码片段添加到您的 pom.xml 文件中。

 <packaging>jar</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>

    <build>
        <plugins>

        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>com.validator.App</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>