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

Java -jar "app.jar"

我得到了以下信息:

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

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


当前回答

只需将其添加到java模块的build.gradle中。它将创建可执行jar。 它将包含存档中的依赖库。

jar {
  manifest { 
    attributes "Main-Class": "com.company.application.Main"
  }  

  from {
    configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
  }
}

这将导致[module_name]/build/libs/[module_name].jar文件。 我用shell进行了测试。

其他回答

如果你的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>

找到了一个很好的解决方案,在任何这种情况下都会有所帮助,因为你只需要一个可运行的罐子,这是你在大多数情况下所做的。 如果您的应用程序在Intellij Idea中运行,请遵循以下步骤: 1)进入模块设置和工件,并添加一个jar和定义主类 2)然后在菜单中选择Build,点击“Build artifact”,你就会得到这个罐子。

即使我更改了源文件夹并使用scala而不是java,这种方法仍然有效。

或者,你也可以使用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> 

在这个例子中,节中指定的所有依赖jar都将自动包含在您的单个jar中。注意,jar-with-dependencies应该按字面意思写为,而不是替换为您想要包含的jar文件名。

这是因为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.

Gradle的答案是添加一个jar/manifest/attributes设置,像这样:

apply plugin: 'java'

jar {
    manifest {
        attributes 'Main-Class': 'com.package.app.Class'
    }
}