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

查看:27
本文介绍了在 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 加载主类清单属性

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 清单文件中:

In the src manifest file:

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

但是jar的manifest文件内容是:

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

为什么jar文件MANIFEST.MF中没有添加Main-Class,而src中却有Main-Class?

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 用户界面

在创建工件以创建 jar 文件时,IntelliJ 会自动填充当前清单所在的路径.

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

即在神器创建页面选择Main-Class后,Intellij会自动添加如下路径srcjavaMETA-INFMANIFEST.MF.出于某种原因,构建命令从不采用提到的 MANIFEST.MF 文件.它从头开始创建一个没有主类定义的无法加载主类"

i.e. After choosing Main-Class in the artifact creation page, Intellij will add automatically the following path srcjavaMETA-INFMANIFEST.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.

默认情况示例:

现在这是更改后的样子:

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

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

在 IntelliJ 项目上,打开 .ideaartifacts\_jar.xml

On the IntelliJ project , open .ideaartifacts\_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天全站免登陆