我已经安装了一个应用程序,当我试图运行它(它是一个可执行的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.

其他回答

如果使用Maven,请在pom中包含以下内容

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.2.RELEASE</version>
</parent>

<properties>
    <java.version>1.8</java.version>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

在我的案例中,问题是<build>下的<pluginManagement>使事情不能正常工作。

我原来的pom.xml:

<build>
  <pluginManagement>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        ...
        ...
        ...
  </pluginManagement>
</build>

删除<pluginManagement>后,错误就消失了。

我只想明确一点

Main-Class: <packagename>.<classname>

如果你没有包,你必须忽略这部分,就像这样:

Main-Class: <classname>

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

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

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

欢呼声……