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

Java -jar "app.jar"

我得到了以下信息:

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

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


当前回答

试试这个命令来包含jar:

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

其他回答

如果jar不遵守规则,它就不是一个可执行的jar。

你可以简单地遵循这一步 创建一个jar文件

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

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

 java -cp jarfile-name main-classname

这应该是java -jar app.jar而不是java -jar "app"。

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

如果它不是一个可执行的JAR,那么你需要像这样运行程序:

java -cp app.jar com.somepackage.SomeClass

其中com.somepackage.SomeClass是包含运行程序的主方法的类。(这个类是什么取决于程序,不可能从您提供的信息中判断出来)。

以我为例——我从事的是一个多模块项目——我可以用以下方式介绍这个问题:

我将其添加到父文件pom.xml中,这导致了问题。即,值为true的skip:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <!--
                besides hindering the packaging, this also skips running the app after build when calling spring-boot:run. You have to enable it in the
                corresponding module by setting skip to false, there.
                -->
                <skip>true</skip>
            </configuration>
        </plugin>
    </plugins>
</build>

我修复了这个问题,通过添加相同的配置到模块,我想打包成一个jar,但改变了跳过的值为false:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>${spring-boot.version}</version>
    <configuration>
        <mainClass>${project.mainClass}</mainClass>
        <layout>ZIP</layout>
        <skip>false</skip>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
        </execution>
        <execution>
            <id>build-info</id>
            <goals>
                <goal>build-info</goal>
            </goals>
        </execution>
    </executions>
</plugin>

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