107

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.

3
  • 1
    Do you know build tools like ant, maven or gradle?
    – MariuszS
    Jan 6, 2014 at 15:12
  • I know about them, but so far I have used only make and .NET counterparts (NAnt, MSBuild). Which one of them should I pick if IntelliJ's build tool won't cut it?
    – grudolf
    Jan 6, 2014 at 19:41
  • 1
    Vote to correct this issue here,
    – bigjosh
    Jan 24, 2016 at 19:06

8 Answers 8

275

I had the same problem.

Make sure your MANIFEST.MF is in:

src/main/resources/META_INF/

NOT

src/main/java/META_INF/
6
  • 20
    This is the correct answer indeed, it seems that Intellij IDEA is misplacing the META-INF folder when the project template is Maven
    – Kumait
    Dec 17, 2014 at 23:48
  • In my case I had to change the layout of my project. I previously had a project with maven layout but I removed the support of maven. I recreate a project from scratch and now it works.
    – Maxence
    Jun 16, 2015 at 15:41
  • 1
    This was not working for me, I had to move the Mainfest file to the root of the project.
    – racs
    Jan 13, 2016 at 20:57
  • Is maven so stupid that places this file into wrong dir? :^)
    – Daria
    Apr 11, 2016 at 11:41
  • @Kumait, the issue is still there in IntelliJ 2017.1.5. I'm wondering if JetBrain team is aware of this.
    – fruqi
    Jul 10, 2017 at 9:11
13

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
4
  • 6
    There's no "Use Existing Manifest" button, however the Manifest file in Output Layout is pointing to the correct location (...\src\main\java\META-INF\MANIFEST.MF)
    – grudolf
    Jan 6, 2014 at 19:02
  • 7
    OK, had to delete the jar artifact and recreate it, this time as "Empty" instead of "From modules with dependencies". Got the "Create Manifest" and "Use Existing Manifest" buttons, pointed to my Manifest and readded libraries and compile output. There are two differences from before: a META_INF\MANIFEST.INF is now explicitly included in output layout and the jar file seems to be generated correctly. :)
    – grudolf
    Jan 6, 2014 at 19:06
  • This doesn't solve the problem, at least for me. jamahn's answer did work however. Nov 28, 2014 at 20:49
  • Comment added from low rep user @ ds-justice as follows - @grudolf's comment above about creating an empty Jar was the only thing that worked for me after several hours of struggling with an imported Gradle project. This is an important question, please consider reposting your comment as a separate answer. Nov 12, 2015 at 18:51
12

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>
7

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>
1

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.

1
  • I had this exact situation and reached the same conclusion as you did: newlines between <Dependencies> items worked in Eclipse, but not in Intellij IDEA. Mar 21, 2018 at 11:49
0

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.
0

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!

1
0

Please follow the steps detailed here

https://www.jetbrains.com/help/idea/2022.2/convert-a-regular-project-into-a-maven-project.html#develop_with_maven

Click the Build Project icon to build project. IntelliJ IDEA generates target folder. Note that IntelliJ IDEA only compiles sources and doesn't create either JAR file or Manifest file.

Create a Manifest file in the resources directory.

Right-click the directory, select New | Directory to create the META-INF subdirectory. Then right-click the subdirectory, select New | File to create the MANIFEST.MF file.

the Manifest file Open the MANIFEST.MF file in the editor and add information about your main class.

Check the following code:

Main-Class: com.company.MyApp Alternatively, we can ask Maven to add this line of code into the MANIFEST.MF file with the following code in pom.xml:

org.apache.maven.plugins maven-jar-plugin com.company.MyApp In your POM specify the Manifest file information, so you can use Maven to generate an executable jar file.

POM with manifest Alternatively, you can execute package instead of the install command to achieve the same result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.