Java 无法执行 jar- 文件:“没有主清单属性”

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

Can't execute jar- file: "no main manifest attribute"

javajarmanifestmain

提问by Ewoud

I have installed an application, when I try to run it (it's an executable jar) nothing happens. When I run it from the commandline with:

我已经安装了一个应用程序,当我尝试运行它(它是一个可执行的 jar)时,没有任何反应。当我从命令行运行它时:

java -jar "app.jar"

java -jar "app.jar"

I get the following message:

我收到以下消息:

no main manifest attribute, in "app.jar"

没有主要清单属性,在“app.jar”中

Normally, if I had created the program myself, I would have added a main class attribute to the manifest file. But in this case, since the file is from an application, i cannot do that. I also tried extracting the jar to see if I could find the main class, but there are to many classes and none of them has the word "main" in it's name. There must be a way to fix this because the program runs fine on other systems.

通常,如果我自己创建了程序,我会在清单文件中添加一个主类属性。但在这种情况下,由于文件来自应用程序,我不能这样做。我还尝试提取 jar 以查看是否可以找到主类,但是有很多类,并且它们的名称中都没有“main”这个词。必须有办法解决这个问题,因为该程序在其他系统上运行良好。

回答by John M

If the jar isn't following the rules, it's not an executable jar.

如果 jar 不遵循规则,则它不是可执行 jar。

回答by Jesper

That should have been java -jar app.jarinstead of java -jar "app".

那应该是java -jar app.jar而不是java -jar "app".

The -jaroption only works if the JAR file is an executable JAR file, which means it must have a manifest file with a Main-Classattribute in it. See Packaging Programs in JAR Filesto learn how to create an executable JAR.

-jar选项仅在 JAR 文件是可执行 JAR 文件时才有效,这意味着它必须有一个带有Main-Class属性的清单文件。请参阅在 JAR 文件中打包程序以了解如何创建可执行 JAR。

If it's not an executable JAR, then you'll need to run the program with something like:

如果它不是可执行的 JAR,那么您需要使用以下内容运行该程序:

java -cp app.jar com.somepackage.SomeClass

where com.somepackage.SomeClassis the class that contains the mainmethod to run the program. (What that class is depends on the program, it's impossible to tell from the information you've supplied).

哪里com.somepackage.SomeClass是包含main运行程序的方法的类。(该类是什么取决于程序,从您提供的信息中无法判断)。

回答by Olivier Refalo

First, it's kind of weird, to see you run java -jar "app"and not java -jar app.jar

首先,这有点奇怪,看到你跑java -jar "app"而不是java -jar app.jar

Second, to make a jar executable... you need to jar a file called META-INF/MANIFEST.MF

其次,要使 jar 可执行...您需要 jar 一个名为 META-INF/MANIFEST.MF 的文件

the file itself should have (at least) this one liner:

文件本身应该有(至少)这一行:

Main-Class: com.mypackage.MyClass

Where com.mypackage.MyClassis the class holding the public static void main(String[] args)entry point.

com.mypackage.MyClass持有public static void main(String[] args)入口点的类在哪里。

Note that there are several ways to get this done either with the CLI, Maven, Ant or Gradle:

请注意,有多种方法可以使用 CLI、Maven、Ant 或 Gradle 完成此操作:

For CLI, the following command will do: (tks @dvvrt) jar cmvf META-INF/MANIFEST.MF <new-jar-filename>.jar <files to include>

对于CLI,以下命令将执行: (tks @ dvvrt) jar cmvf META-INF/MANIFEST.MF <new-jar-filename>.jar <files to include>

For Maven, something like the following snippet should do the trick. Note that this is only the plugin definition, not the full pom.xml:

对于Maven,类似于以下代码段的内容应该可以解决问题。请注意,这只是插件定义,而不是完整的pom.xml

<build>
  <plugins>
    <plugin>
      <!-- Build an executable JAR -->
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <version>3.1.0</version>
      <configuration>
        <archive>
          <manifest>
            <addClasspath>true</addClasspath>
            <classpathPrefix>lib/</classpathPrefix>
            <mainClass>com.mypackage.MyClass</mainClass>
          </manifest>
        </archive>
      </configuration>
    </plugin>
  </plugins>
</build>

(Pick a <version>appropriate to your project.)

(选择一个<version>适合你的项目。)

For Ant, the snippet below should help:

对于Ant,下面的代码片段应该会有所帮助:

<jar destfile="build/main/checksites.jar">
  <fileset dir="build/main/classes"/>
  <zipfileset includes="**/*.class" src="lib/main/some.jar"/>
  <manifest>
    <attribute name="Main-Class" value="com.acme.checksites.Main"/>
  </manifest>
</jar>

Credits Michael Niemand -

致谢迈克尔·尼曼德 -

For Gradle:

对于摇篮

plugins {
    id 'java'
}

jar {
    manifest {
        attributes(
                'Main-Class': 'com.mypackage.MyClass'
        )
    }
}

回答by Shekh Akther

Any executable jar file Should run either by clicking or running using command prompt like java -jar app.jar (use "if path of jar contains space" - i.e. java -jar "C:\folder name\app.jar"). If your executable jar is not running, which means it is not created properly.

任何可执行的 jar 文件都应该通过单击或使用命令提示符运行,如 java -jar app.jar(使用“如果 jar 的路径包含空格” - 即 java -jar “C:\文件夹名称\app.jar”)。如果您的可执行 jar 没有运行,这意味着它没有正确创建。

For better understanding, extract the jar file (or view using any tool, for windows 7-Zip is nice one) and check the file under /META-INF/MANIFEST.MF. If you find any entry like

为了更好地理解,提取 jar 文件(或使用任何工具查看,对于 windows 7-Zip 是很好的一个)并检查 /META-INF/MANIFEST.MF 下的文件。如果您发现任何条目,例如

Main-Class: your.package.name.ClaaswithMain - then it's fine, otherwise you have to provide it.

Main-Class: your.package.name.ClaaswithMain - 那么就可以了,否则你必须提供它。

Be aware of appending Main-Class entry on MANIFEST.MF file, check where you are saving it!

请注意在 MANIFEST.MF 文件上附加 Main-Class 条目,检查您将其保存在哪里!

回答by Dave

For maven, this is what solved it (for me, for a Veetle codebase on GitHub):

对于 maven,这就是解决它的方法(对我来说,对于 GitHub 上的 Veetle 代码库):

<build>
<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.0</version>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>
        <configuration>
          <transformers>
            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
              <mainClass>org.lazydevs.veetle.api.VeetleAPI</mainClass>
            </transformer>
          </transformers>
        </configuration>
      </execution>
    </executions>
  </plugin>
 </plugins>
</build>

Cheers...

干杯...

回答by Burhan ARAS

Try this command to include the jar:

试试这个命令来包含 jar:

java -cp yourJarName.jar your.package..your.MainClass

回答by satheesh.v

You might not have created the jar file properly:

您可能没有正确创建 jar 文件:

ex: missing option m in jar creation

例如:在 jar 创建中缺少选项 m

The following works:

以下工作:

jar -cvfm MyJar.jar Manifest.txt *.class

回答by Koneri

You Can Simply follow this step Create a jar file using

您可以简单地按照此步骤创建一个 jar 文件使用

 jar -cfm jarfile-name manifest-filename Class-file name

While running the jar file simple run like this

运行 jar 文件时,像这样简单运行

 java -cp jarfile-name main-classname

回答by Sasanka Panguluri

That is because Java cannot find the Main attribute in the MANIFEST.MF file. The Main attribute is necessary to tell java which class it should use as the application's entry point. Inside the jar file, the MANIFEST.MF file is located in META-INF folder. Wondering how you could look at what's inside a jar file? Open the jar file with WinRAR.

这是因为 Java 在 MANIFEST.MF 文件中找不到 Main 属性。Main 属性对于告诉 java 它应该使用哪个类作为应用程序的入口点是必要的。在 jar 文件中,MANIFEST.MF 文件位于 META-INF 文件夹中。想知道如何查看 jar 文件中的内容?用 WinRAR 打开 jar 文件。

The main attribute inside the MANIFEST.MF looks like this:

MANIFEST.MF 中的主要属性如下所示:

Main-Class: <packagename>.<classname>

You get this "no main manifest attribute" error when this line is missing from the MANIFEST.MF file.

当 MANIFEST.MF 文件中缺少此行时,您会收到此“无主清单属性”错误。

It's really a huge mess to specify this attribute inside the MANIFEST.MF file.

在 MANIFEST.MF 文件中指定这个属性真的是一团糟。

Update: I just found a really neat way to specify the Application's entry point in eclipse. When you say Export,

更新:我刚刚找到了一种在 eclipse 中指定应用程序入口点的非常巧妙的方法。当你说出口时,

Select Jar and next 

[ give it a name in the next window ] and next

and next again

and you'll see " Select the class of the application entry point".

Just pick a class and Eclipse will automatically build a cool MANIFEST.MF for you.

enter image description here

在此处输入图片说明

回答by CodeBrew

Alternatively, you can use maven-assembly-plugin, as shown in the below example:

或者,您可以使用 maven-assembly-plugin,如下例所示:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>single</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <archive>
        <manifest>
          <addClasspath>true</addClasspath>
          <mainClass>com.package.MainClass</mainClass>
        </manifest>
      </archive>
      <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
      </descriptorRefs>
    </configuration>
  </plugin> 

In this example all the dependency jars as specified in section will be automatically included in your single jar. Note that jar-with-dependencies should be literally put as, not to be replaced with the jar file names you want to include.

在此示例中,部分中指定的所有依赖 jar 将自动包含在您的单个 jar 中。请注意,jar-with-dependencies 应按字面意思放置,而不是替换为您要包含的 jar 文件名。