简体   繁体   English

在IntelliJ IDEA中创建一个可执行的jar

[英]Create an excutable jar in IntelliJ IDEA

I am using IntelliJ IDEA 12.1.3. 我正在使用IntelliJ IDEA 12.1.3。 I have one module and I am trying to convert it into a jar as per the steps given here . 我有一个模块,我正在尝试按照此处给出的步骤将其转换为jar。 I built the jar file and I moved the jar file to D:/. 我构建了jar文件,然后将jar文件移到了D:/。 If I try to run this jar I get the following message: 如果我尝试运行此jar,则会收到以下消息:

Failed to load Main-Class manifest attribute from jar 无法从jar加载Main-Class清单属性

If I run it using java -classpath jar com.code.TestCase it works perfectly. 如果我使用java -classpath jar com.code.TestCase运行它,它会完美运行。

Why does this happen? 为什么会这样? I created the jar using IntelliJ IDEA. 我使用IntelliJ IDEA创建了jar。 In that jar it has all the code for all external libraries which I added. 在那个jar中,它具有我添加的所有外部库的所有代码。

In the src manifest file: 在src清单文件中:

Manifest-Version: 1.0
Main-Class: com.code.TestCase

but the jar's manifest file content is: 但是jar的清单文件内容为:

Manifest-Version: 1.0
Build-Jdk: 1.6.0
Built-By: ubuntu
Created-By: Apache Maven
Archiver-Version: Plexus Archiver

Name: org.apache.log4j
Implementation-Vendor: "Apache Software Foundation"
Implementation-Title: log4j
Implementation-Version: 1.2.15

Why is Main-Class not added in the jar file MANIFEST.MF, but it has Main-Class in src? 为什么没有在jar文件MANIFEST.MF中添加Main-Class ,但是在src中包含Main-Class

I have experienced the same issue and found a fix and/or bug in how Intellij auto populates the path for the manifest. 我遇到了同样的问题,并发现了Intellij如何自动填充清单路径的修复程序和/或错误。

INTELLIJ UI 智能用户界面

When creating an artifact to create jar files, IntelliJ auto populates the the path where the current manifest is located. 创建工件以创建jar文件时,IntelliJ会自动填充当前清单所在的路径。

ie After choosing Main-Class in the artifact creation page, Intellij will add automatically the following path \\src\\java\\META-INF\\MANIFEST.MF. 例如,在工件创建页面中选择Main-Class之后,Intellij将自动添加以下路径\\ src \\ java \\ META-INF \\ MANIFEST.MF。 For some reason, the build command never takes the mentioned MANIFEST.MF File. 出于某种原因,build命令从不采用提到的MANIFEST.MF文件。 It creates one from scratch without the Main-Class definition which results on the "Failed to load Main-Class" 它从头开始创建一个没有Main-Class定义的内容,这导致“无法加载Main-Class”

If using the IntelliJ UI to create jar artifacts; 如果使用IntelliJ UI创建jar工件; removed the auto populate path by changing it to /$PROJECT_DIR/src/ and click OK. 通过将自动填充路径更改为/$PROJECT_DIR/src/来删除该自动填充路径,然后单击“确定”。

Example of what it looks like by default: 默认情况下的示例:

默认的IntelliJ Jar工件设置

Now this is what it should look like after the change: 现在这是更改后的样子:

固定的IntelliJ工件设置

MANUAL FIX TO ENTER CORRECT MANIFEST.MF FILE 手动修复以输入正确的MANIFEST.MF文件

On the IntelliJ project , open .idea\\artifacts\\_jar.xml 在IntelliJ项目上,打开.idea \\ artifacts \\ _jar.xml

Make sure it contains 确保包含

  <element id="directory" name="META-INF">
    <element id="file-copy" path="$PROJECT_DIR$/src/META-INF/MANIFEST.MF" />
  </element>

You might need to reload intelliJ once you do this so it picks up on the changes to the .xml file 一旦执行此操作,您可能需要重新加载intelliJ,以便它可以对.xml文件进行更改

For me it happened on maven projects where a different structure exists (ie src/main/java/..). 对我来说,这发生在存在不同结构的Maven项目中(即src / main / java / ..)。

I am using OSX 10.9.2 with IntelliJ version 12.1.6 我正在将OSX 10.9.2与IntelliJ 12.1.6版一起使用

Hope that helps. 希望能有所帮助。

One of the important rule is that Manifest file must end with new line or carriage return character. 重要规则之一是清单文件必须以换行符或回车符结尾。 If it is not there then add now. 如果不存在,请立即添加。

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

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