我已经安装了一个应用程序,当我试图运行它(它是一个可执行的jar)什么都没有发生。当我从命令行运行它时:

Java -jar "app.jar"

我得到了以下信息:

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

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


当前回答

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

MANIFEST中的主属性。MF是这样的:

Main-Class: <packagename>.<classname>

当manifest中缺少这一行时,您会得到这个“no main manifest attribute”错误。MF文件。

在MANIFEST中指定这个属性真的很麻烦。MF文件。

更新:我刚刚找到了一种非常简洁的方法来在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.

其他回答

我试过了,对我很有效。 MVN清洁安装包应该工作。

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
            <execution>
                <goals>
                    <goal>repackage</goal>
                </goals>
            </execution>
            </executions>
        </plugin>
    </plugins>
</build>

因为您已经添加了MANIFEST。MF,我认为你应该考虑这个文件中Field的顺序。我的env是java版本“1.8.0_91”

还有我的舱单。MF为这里

// MANIFEST.MF
Manifest-Version: 1.0
Created-By: 1.8.0_91 (Oracle Corporation)
Main-Class: HelloWorldSwing

// run
~ java -jar HelloWorldSwing.jar
no main manifest attribute, in HelloWorldSwing.jar

然而,这如下所述

Manifest-Version: 1.0
Main-Class: HelloWorldSwing
Created-By: 1.8.0_91 (Oracle Corporation)

//this run swing normally

如果你的on maven和pom。xml是这样的

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.3</version>
        <relativePath/> <!-- lookup parent from repository -->
      </parent>
      <groupId>com.example</groupId>
      <artifactId>demo</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <name>demo</name>
      <properties>
        <java.version>11</java.version>
      </properties>
      <dependencies>
        <!-- dependencies -->
      </dependencies>
      <build>
        <pluginManagement>
          <plugins>
            <plugin>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
          </plugins>
        </pluginManagement>
      </build>
    </project>

只需注释pluginManagement,就会得到下面的pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.3</version>
        <relativePath/> <!-- lookup parent from repository -->
      </parent>
      <groupId>com.example</groupId>
      <artifactId>demo</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <name>demo</name>
      <properties>
        <java.version>11</java.version>
      </properties>
      <dependencies>
        <!-- dependencies -->
      </dependencies>
      <build>
        <plugins>
          <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
          </plugin>
        </plugins>
      </build>
    </project>

我也有同样的问题。这里提到的许多解决方案并没有提供完整的内容,因此我将尝试向您概述如何从命令行打包jar文件。

如果您想在包中包含.class文件,请将包添加在.java. xml文件的开头。 Test.java 包testpackage; 公共类测验 { ... } 使用包名给出的结构来编译你的.class文件的代码: Javac -d。Test.java d。使编译器创建所需的目录结构。 在打包.jar文件时,您需要指示jar例程如何打包它。这里我们使用选项集cvfeP。这是为了保持包结构(选项P),指定入口点,以便清单文件包含有意义的信息(选项e)。选项f允许您指定文件名,选项c创建一个存档,选项v将输出设置为详细。这里需要注意的重要事项是P和e。 然后是我们想要的jar的名称test.jar。 然后是入口点。 然后是-C。<packagename>/从该文件夹中获取类文件,并保留文件夹结构。 jar cvfeP test.jar testpackage. jar测试-C。testpackage / 检查压缩程序中的.jar文件。它应该具有以下结构 test.jar meta - inf |清单。曼氏金融 testpackage | Test.class 清单。MF应该包含以下内容 Manifest-Version: 1.0 创建对象:JDK版本> (Oracle Corporation) 主类:testpackage。测试 如果你手动编辑你的清单,一定要保持换行符在最后,否则java不会识别它。 执行你的.jar文件 Java -jar test.jar

(第一个帖子-所以可能不干净)

这是我对OS X 11.6的修复,基于maven的Netbeans 8.2程序。到目前为止,我的应用程序是100%的Netbeans -没有调整(只是一些shell逃脱为不可能!)。

在这里和其他地方尝试了几乎所有的答案都无济于事之后,我回到了“使用有用的东西”的艺术上。

上面的答案(olivier-refalo,谢谢)看起来是正确的起点,但并没有帮助。

看看其他成功的项目,我注意到清单行中有一些微小的差异:

addClasspath, classpathPrefix不存在(已删除) mainClass缺少了“com”。(使用NB ->项目 属性->运行->主类->浏览指定)

不知道为什么(我只有3个月的java)或如何,但只能说这是有效的。

下面是修改后的manifest块:

    <manifest>
        <mainClass>mypackage.MyClass</mainClass>
    </manifest>