在 IntelliJ IDEA 中创建的.jar 中错误的 Manifest.mf

我正在尝试将一个使用 OptaPlanner6.0.1库的项目打包到一个。Jar 通过 IntelliJ IDEA 的 jar 工件,而不是包含标准的 Manif.mf

Manifest-Version: 1.0
Main-Class: a.b.c.app

Jar 使用 ecj-3.7.2.jar 中提供的那个,它是 OptaPlanner 的支持库之一:

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

因此,在尝试运行应用程序时会发生 "no main manifest attribute, in appname.jar"错误。如果我手动替换。我的一切正常工作的 jar 文件。我能做些什么来弥补这一切吗?

我将这些库保存在一个单独的/lib 目录中,并将它们添加到 jar 工件的根目录中,作为 Extract Directory,IntelliJ IDEA 是 v13.0.1。

86129 次浏览

To fix:

  1. File > Project Structure
  2. Under Project Settings on the left, select "Artifacts"
  3. Find the JAR definition In the middle pane and select it
  4. In the left pane of the "Output Layout" tab find the jar file in the list and select it
  5. At the bottom, click the "Use Existing Manifest" button and select the manifest file that is in your project source.
  6. Click OK and run the build

There are several ways to generate executable jars. Using IntelliJ's GUI feature is one good way. Another way is to use Maven (or similarly in gradle, buildr, etc) which is build-server friendly:

It's more or less copy pasteable from the optaplanner examples maven build:

  1. The end-user jar (optaplanner-examples-*.jar) must include the classpath of its dependencies in its manifest.
  2. The sh and bat script must then run that jar with accordingly.

I had the same problem.

Make sure your MANIFEST.MF is in:

src/main/resources/META_INF/

NOT

src/main/java/META_INF/

I had a similar problem.

The problem was in file pom.xml.

<archive>
<manifestEntries>
<Dependencies>one.jar,
two.rar,
other.jar
</Dependencies>
</manifestEntries>
</archive>

I do not know for what reason this code works in eclipse, but not in IntelliJ

This it correct.

<archive>
<manifestEntries>
<Dependencies>one.jar, two.rar, other.jar</Dependencies>
</manifestEntries>
</archive>

Manifest.mf worked!!!

I hope this helps.

As noted in @grudolf's comment in one of the other answers, one way to do this (and the only one that worked for me in an imported Gradle project) is to create an empty jar as follows:

  • Project Structure -> Artifacts -> + Jar -> Empty
  • Centre pane now has Create Manifest and Use Existing Manifest buttons. Use one of these.
  • I had difficulty if I extracted dependent libraries with their own manifests into the output root, they seemed to intermittently overwrite the new manually created manifest. Messing around with order of operations seemed to make it work.

UPDATE:

This is definitely a bug in Idea. This linked answer works reliably when there are extracted directories. In essence, you find your .idea/JARNAME.xml, add add the following element to the very top of the <root> element for your jar. Any extracted elements above your new file-copy that contain a manifest will clobber your new manifest.

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

If you want to specify Main Class, you have to add this plugin to pom.xml:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifest>
<mainClass>Form</mainClass>
</manifest>
</archive>
</configuration>
</plugin>

To have no problem like Manifest, you should have a directory named "META-INF" in "src" directory. So, create it and put a file named "MANIFEST.MF" in it with the following contents:

Manifest-Version: 1.0
Main-Class: <packageName>.Main

Not to forgot to replace the package's name containing Main class above!