Wrong Manifest.mf in IntelliJ IDEA created .jar

I'm trying to package a project using OptaPlanner 6.0.1 libraries into a .jar through IntelliJ IDEA's jar artifact but instead of my manifest.mf containing the standard

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

the jar uses the one supplied in ecj-3.7.2.jar, one of OptaPlanner's supporting libraries:

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

Because of this, a "no main manifest attribute, in appname.jar" error happens when trying to run the app. If I manually replace the manifest in .jar file with mine everything works correctly. Is there anything I can do to fix this?

I keep the libraries in a separate /lib directory and they were added to the root of jar artifact as Extracted Directory, IntelliJ IDEA is v13.0.1.


Solution 1:

I had the same problem.

Make sure your MANIFEST.MF is in:

src/main/resources/META_INF/

NOT

src/main/java/META_INF/

Solution 2:

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

Solution 3:

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>