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

Can#39;t execute jar- file: quot;no main manifest attributequot;(无法执行 jar- 文件:“没有主要清单属性)

本文介绍了无法执行 jar- 文件:“没有主要清单属性"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

java -jar "app.jar"

java -jar "app.jar"

我收到以下消息:

没有主清单属性,在app.jar"中

no main manifest attribute, in "app.jar"

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

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.

推荐答案

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

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

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

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

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

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

Main-Class: com.mypackage.MyClass

其中 com.mypackage.MyClass 是持有 public static void main(String[] args) 入口点的类.

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

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

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

对于 CLI,可以使用以下命令:(tks @dvvrt)

For CLI, the following command will do: (tks @dvvrt)

jar cmvf META-INF/MANIFEST.MF <new-jar-filename>.jar  <files to include>

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

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

关于这个插件的最新文档:参见 https://maven.apache.org/插件/maven-jar-plugin/

Latest doc on this plugin: see https://maven.apache.org/plugins/maven-jar-plugin/

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

(选择一个<version> 适合您的项目.)

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

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

For Ant, the snippet below should help:

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

对于Gradle:

plugins {
    id 'java'
}

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

这篇关于无法执行 jar- 文件:“没有主要清单属性"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:无法执行 jar- 文件:“没有主要清单属性"

基础教程推荐