简体   繁体   English

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

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

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 中运行良好:

主要方法在类中

What do I need to do to successfully run this file from the command line?我需要做什么才能从命令行成功运行这个文件?

尝试运行

java -cp ScrumTimeCaptureMaintenence.jar Main

In Eclipse: right-click on your project -> Export -> JAR file在 Eclipse 中: right-click您的项目 -> Export -> JAR file

At last page with options (when there will be no Next button active) you will see settings for Main class: .在带有选项的最后一页(当没有Next按钮处于活动状态时)您将看到Main class:设置Main class: You need to set here class with main method which should be executed by default (like when JAR file will be double-clicked).您需要在此处设置默认执行的main方法的类(例如双击 JAR 文件时)。

The -jar option only works if the JAR file is an executable JAR file, which means it must have a manifest file with a Main-Class attribute 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.SomeClass is the class that contains the main method to run the program.其中com.somepackage.SomeClass是包含运行程序的main方法的类。

You can select the "Runnable JAR File" after you click on "Export".单击“导出”后,您可以选择“可运行的 JAR 文件”。

You can specify your main driver in "Launch Configuration"您可以在“启动配置”中指定您的主驱动程序

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

First run your application from eclipse to create launch configuration.首先从 Eclipse 运行您的应用程序以创建启动配置。 Then just follow the steps:然后只需按照以下步骤操作:

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

Source: Creating a New Runnable JAR File at Eclipse.org来源:在 Eclipse.org 创建一个新的可运行 JAR 文件

Export you Java Project as an Runnable Jar file rather than Jar.将您的 Java 项目导出为Runnable Jar 文件而不是 Jar。

I exported my project as Jar and even though the Manifest was present it gave me the error no main manifest attribute in jar even though the Manifest file was present in the Jar.我将我的项目导出为Jar ,即使 Manifest 存在,它也给了我jar 中没有主要清单属性的错误即使 Manifest 文件存在于 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然而,清单文件中只有一个条目,它没有指定要执行的 Main 类或函数或任何依赖的 JAR

After exporting it as Runnable Jar file it works as expected.将其导出为Runnable Jar 文件后,它按预期工作。

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 !!干杯!!

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM