在IntelliJ IDEA中创建一个可删除的jar [英] Create an excutable jar in IntelliJ IDEA

查看:281
本文介绍了在IntelliJ IDEA中创建一个可删除的jar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用IntelliJ IDEA 12.1.3。我有一个模块,我正在尝试按照给出的步骤将其转换为jar这里。我构建了jar文件,并将jar文件移动到D:/。如果我尝试运行此jar,我会收到以下消息:

I am using 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. I built the jar file and I moved the jar file to D:/. If I try to run this jar I get the following message:


无法从jar加载Main-Class清单属性

Failed to load Main-Class manifest attribute from jar

如果我使用 java -classpath jar com.code.TestCase 运行它,它可以很好地工作。

If I run it using java -classpath jar com.code.TestCase it works perfectly.

为什么会这样?我使用IntelliJ IDEA创建了jar。在那个jar中它包含我添加的所有外部库的所有代码。

Why does this happen? I created the jar using IntelliJ IDEA. In that jar it has all the code for all external libraries which I added.

在src清单文件中:

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

但是jar的清单文件内容是:

but the jar's manifest file content is:

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

为什么 Main-Class 未添加到jar文件MANIFEST.MF中,但它有 Main -class 在src?

Why is Main-Class not added in the jar file MANIFEST.MF, but it has Main-Class in src?

推荐答案

我遇到了同样的问题并找到了修复和/或Intellij如何自动填充清单路径的错误。

I have experienced the same issue and found a fix and/or bug in how Intellij auto populates the path for the manifest.

INTELLIJ UI

创建时要创建jar文件的工件,IntelliJ auto会填充当前清单所在的路径。

When creating an artifact to create jar files, IntelliJ auto populates the the path where the current manifest is located.

即。在工件创建页面中选择Main-Class后,Intellij将自动添加以下路径\src\java\META-INF \ MANIFEST.MF。出于某种原因,构建命令永远不会采用上面提到的MANIFEST.MF文件。它从头创建一个没有Main-Class定义,导致无法加载Main-Class

i.e. After choosing Main-Class in the artifact creation page, Intellij will add automatically the following path \src\java\META-INF\MANIFEST.MF. For some reason, the build command never takes the mentioned MANIFEST.MF File. It creates one from scratch without the Main-Class definition which results on the "Failed to load Main-Class"

如果使用IntelliJ UI创建jar工件;删除自动填充路径,方法是将其更改为 / $ PROJECT_DIR / src / ,然后单击确定。

If using the IntelliJ UI to create jar artifacts; removed the auto populate path by changing it to /$PROJECT_DIR/src/ and click OK.

示例它看起来像默认情况:

Example of what it looks like by default:

现在这是改变后的样子:

Now this is what it should look like after the change:

手动修复输入正确的MANIFEST.MF文件

在IntelliJ项目中,打开.idea \artifacts \ __jar.xml

On the IntelliJ project , open .idea\artifacts\_jar.xml

确保它包含

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

一旦你这样做,你可能需要重新加载intelliJ,以便它接收对.xml的更改文件

You might need to reload intelliJ once you do this so it picks up on the changes to the .xml file

对我而言,它发生在存在不同结构的maven项目中(即src / main / java /..).

For me it happened on maven projects where a different structure exists (i.e. src/main/java/..).

我使用OSX 10.9.2和IntelliJ版本12.1.6

I am using OSX 10.9.2 with IntelliJ version 12.1.6

希望有所帮助。

这篇关于在IntelliJ IDEA中创建一个可删除的jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆