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

Java -jar "app.jar"

我得到了以下信息:

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

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


当前回答

我个人认为这里所有的答案都是对问题的误解。这个问题的答案在于spring-boot构建.jar的方式不同。每个人都知道Spring Boot设置了一个这样的清单,这与每个人都认为这是一个标准的.jar启动不同,这可能是也可能不是:

Start-Class: com.myco.eventlogging.MyService
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Spring-Boot-Version: 1.4.0.RELEASE
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_131
Main-Class: org.springframework.boot.loader.JarLauncher

也许它需要在类路径上使用org.springframework.boot.loader.JarLauncher来执行?

其他回答

对我来说,发生这个错误仅仅是因为我忘记告诉Eclipse我想要一个可运行的jar文件,而不是一个简单的库jar文件。因此,当您在Eclipse中创建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>

欢呼声……

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

检查你的jar文件MANIFEST。MF Main-Class是否可用

first.java

class first
{
        public static void main (String arg[ ])
        {
           System.out.println("Welcome to the world of Java");
        }
}

之前:

Manifest-Version: 1.0
Created-By: 1.7.0_80 (Oracle Corporation)

sony@sony-VPCEH25EN:~/Documents$ java -jar first.jar
no main manifest attribute, in first.jar

后:

Manifest-Version: 1.0
Created-By: 1.7.0_80 (Oracle Corporation)
Main-Class: first

sony@sony-VPCEH25EN:~/Documents$ java -jar first.jar 
Welcome to the world of Java

我刚才得到了同样的错误。 如果你正在使用gradle,只需在你的gradle.build中添加下一个:

apply plugin: 'java'

jar {
    manifest {
        attributes 'Main-Class': 'com.company.project.MainClass'
    }
}

其中com.company.project.MainClass使用公共静态void main(String[] args)方法的类的路径。