Java 无法从命令行运行 jar 文件:“没有主要清单属性”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19250966/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 15:27:55  来源:igfitidea点击:

Failing to run jar file from command line: “no main manifest attribute”

javaeclipse

提问by David Tunnell

I am trying to run a jar file from the console:

我正在尝试从控制台运行 jar 文件:

java -jar ScrumTimeCaptureMaintenence.jar

And am getting the error:

并且我收到错误:

Can't execute jar- file: “no main manifest attribute”

无法执行 jar- 文件:“没有主清单属性”

As you can see I do in fact have a main file and it runs fine from eclipse:

正如你所看到的,我实际上有一个主文件,它在 eclipse 中运行良好:

main method is in class

主要方法在类中

What do I need to do to successfully run this file from the command line?

我需要做什么才能从命令行成功运行这个文件?

采纳答案by Milan Baran

Try to run

尝试运行

java -cp ScrumTimeCaptureMaintenence.jar Main

回答by Admit

First run your application from eclipse to create launch configuration. Then just follow the steps:

首先从 Eclipse 运行您的应用程序以创建启动配置。然后只需按照以下步骤操作:

  1. From the menu bar's Filemenu, select Export.
  2. Expand the Javanode and select Runnable JAR file. Click Next.
  3. In the Runnable JAR File Specificationpage, select a 'Java Application' launch configuration to use to create a runnable JAR.
  4. In the Export destinationfield, either type or click Browseto select a location for the JAR file.
  5. Select an appropriate library handlingstrategy.
  6. Optionally, you can also create an ANT script to quickly regenerate a previously created runnable JAR file.
  1. 从菜单栏的文件菜单中,选择导出
  2. 展开Java节点并选择Runnable JAR file。单击下一步
  3. Runnable JAR File Specification页面中,选择一个“Java 应用程序”启动配置来创建一个可运行的 JAR。
  4. 导出目标字段中,输入或单击浏览以选择 JAR 文件的位置。
  5. 选择合适的库处理策略。
  6. 或者,您还可以创建一个 ANT 脚本来快速重新生成以前创建的可运行 JAR 文件。

Source: Creating a New Runnable JAR Fileat Eclipse.org

来源:在 Eclipse.org创建一个新的可运行 JAR 文件

回答by Pshemo

In Eclipse: right-clickon your project -> Export-> JAR file

在 Eclipse 中:right-click在您的项目上 -> Export->JAR file

At last page with options (when there will be no Nextbutton active) you will see settings for Main class:. You need to set here class with mainmethod which should be executed by default (like when JAR file will be double-clicked).

在带有选项的最后一页(当没有Next按钮处于活动状态时)您将看到Main class:. 您需要在此类中设置main默认执行的方法(例如双击 JAR 文件时)。

回答by Fever

You can select the "Runnable JAR File" after you click on "Export".

单击“导出”后,您可以选择“可运行的 JAR 文件”。

You can specify your main driver in "Launch Configuration"

您可以在“启动配置”中指定您的主驱动程序

enter image description hereenter image description here

在此处输入图片说明在此处输入图片说明

回答by Rajat

Export you Java Project as an Runnable Jar filerather than Jar.

将您的 Java 项目导出为Runnable Jar 文件而不是 Jar。

I exported my project as Jarand even though the Manifest was present it gave me the error no main manifest attribute in jareven though the Manifest file was present in the Jar. However there is only one entry in the Manifest file and it didn't specify the Main class or Function to execute or any dependent JAR's

我将我的项目导出为Jar,即使 Manifest 存在,它也给了我jar 中没有主要清单属性的错误即使 Manifest 文件存在于 Jar 中。然而,清单文件中只有一个条目,它没有指定要执行的 Main 类或函数或任何依赖的 JAR

After exporting it as Runnable Jar fileit works as expected.

将其导出为Runnable Jar 文件后,它按预期工作。

回答by Dheeraj Kumar

You need to include "Main class" attribute in Manisfest.mf file in Jar

您需要在 Jar 的 Manisfest.mf 文件中包含“Main class”属性

For example: Main-Class: MyClassName

例如:主类:MyClassName

Second thing, To add Manifest file in Your jar, You can manually create file in your workspace folder, and refresh in Eclipse Project explorer.

第二件事,要在您的 jar 中添加清单文件,您可以在工作区文件夹中手动创建文件,并在 Eclipse 项目资源管理器中刷新。

While exporting, Eclipse will create a Jar which will include your manifest.

导出时,Eclipse 将创建一个包含您的清单的 Jar。

Cheers !!

干杯!!

回答by spacewanderer

The -jaroption only works if the JAR file is an executable JAR file, which means it must have a manifest file with a Main-Classattribute in it.

-jar选项仅在 JAR 文件是可执行 JAR 文件时才有效,这意味着它必须有一个带有Main-Class属性的清单文件。

If it's not an executable JAR, then you'll need to run the program with something like:

如果它不是可执行的 JAR,那么您需要使用以下内容运行该程序:

java -cp app.jar com.somepackage.SomeClass

where com.somepackage.SomeClassis the class that contains the mainmethod to run the program.

哪里com.somepackage.SomeClass是包含main运行程序的方法的类。

回答by Hamid Mohayeji

If you are using Spring boot, you should add this plugin in your pom.xml:

如果您使用的是 Spring Boot,您应该在您的pom.xml:

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