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

其他回答

这是因为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的问题在于它试图包括第一个MANIFEST。MF文件从第一个库从依赖而不是我们自己的清单。Mf当你使用神器的时候!

将jar.jar重命名为jar.zip 打开清单。MF文件从META-INF\MANIFEST。曼氏金融 复制真实的清单。MAVEN已经在你的项目中生成了MF 其中包括: Manifest-Version: 1.0 主类:yourpacket。你的主类(例如info.data.MainClass) 替换MANIFEST的内容。你罐子里的MF。 将jar.zip重命名为jar.jar。 现在java -jar yourjar.jar工作得很完美。

OR!

简单地创建您自己的MANIFEST。MF和:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>3.2.0</version>
    <configuration>
        <archive>
            <manifestFile> Your path like: src/main/resources/META-INF/MANIFEST.MF </manifestFile>
            <index>true</index>
                <manifest>
                    <addClasspath>true</addClasspath>
                </manifest>
        </archive>
    </configuration>
</plugin>

但是如果您使用maven面板(或maven命令行),您可以强制它生成自己的清单并将其包含到JAR文件中。

将以下代码添加到you pom.xml的构建部分: <插件> <插件> < groupId > org.apache.maven.plugins < / groupId > < artifactId > maven-assembly-plugin < / artifactId > <版本> 3.2.0 > < /版本 执行< > 执行< > <阶段>包> < /阶段 <目标> <目标>单> < /目标 > < /目标 执行< / > > < /执行 < >配置 < descriptorRefs > < descriptorRef > jar-with-dependencies < / descriptorRef > < / descriptorRefs > <文件> 真正<指数> < /指数> <清单> 真正< addClasspath > < / addClasspath > < mainClass > yourpacket。yourmainclass(例如info.data.MainClass)</mainClass> . /mainClass < /清单> < manifestEntries > < >发展模式> < /模式 < url > $ {project.url} < / url > < / manifestEntries > < / >存档 < /配置> < /插件> 打开MAVEN面板(在Intellij中)并执行“Install”。它将生成MANIFEST文件,并将包含所有依赖项的JAR文件属性编译到“Target”文件夹中。它还将被安装到本地maven存储库中。

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

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

只需将其添加到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进行了测试。