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

Java -jar "app.jar"

我得到了以下信息:

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

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


当前回答

在您的本地.m2目录中检查该工件的子目录。 如果存在-删除它,并再次执行Maven更新

其他回答

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

我今天也遇到了同样的问题。我的问题解决了,我移动META-INF到资源文件夹。

如果您正在使用命令行组装.jar,则可以在不添加清单文件的情况下指向主文件。例子:

jar cfve app.jar TheNameOfClassWithMainMethod *.class

(参数"e"是这样做的:TheNameOfClassWithMainMethod是一个类的名称,带有方法main()和app.jar -可执行文件的名称。jar和*.class -只是所有的类文件组装)

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

我将其添加到父文件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>

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

apply plugin: 'java'

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

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