Wrong Manifest.mf in IntelliJ IDEA created .jar
Asked Answered
B

8

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.

Bundestag answered 6/1, 2014 at 14:58 Comment(3)
Do you know build tools like ant, maven or gradle?Humoral
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?Bundestag
Vote to correct this issue here,Brutish
S
275

I had the same problem.

Make sure your MANIFEST.MF is in:

src/main/resources/META_INF/

NOT

src/main/java/META_INF/
Shakitashako answered 12/1, 2014 at 11:31 Comment(6)
This is the correct answer indeed, it seems that Intellij IDEA is misplacing the META-INF folder when the project template is MavenJoellyn
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.Babel
This was not working for me, I had to move the Mainfest file to the root of the project.Slit
Is maven so stupid that places this file into wrong dir? :^)Tenno
@Kumait, the issue is still there in IntelliJ 2017.1.5. I'm wondering if JetBrain team is aware of this.Foundation
I don't know what's up with my IntelliJI IDEA 2017.2.6. But in my the problem appears with /resources folder also. Keeping it as src/META_INF/ worked for me.Vitamin
K
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
Kresic answered 6/1, 2014 at 16:33 Comment(4)
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)Bundestag
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. :)Bundestag
This doesn't solve the problem, at least for me. jamahn's answer did work however.Soliloquize
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.Zeldazelde
M
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>
Mulford answered 13/11, 2015 at 1:30 Comment(0)
R
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>
Rhoden answered 12/1, 2016 at 13:11 Comment(0)
F
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.

Forceful answered 8/10, 2015 at 12:25 Comment(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.Jacquijacquie
C
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.
Chrysalid answered 7/1, 2014 at 7:48 Comment(0)
H
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!

Hellbent answered 20/12, 2016 at 15:33 Comment(1)
The record shows I trimmed religious material from your post in August. Please note that the community has discussed adding religious themes into posts, and the answer is that we would rather they were left out. You are most welcome to add them into your profile or avatar, though.Poussin
H
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.

Hegyera answered 27/3, 2023 at 14:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.