66

I am getting no main manifest attribute while running the jar create by Intellij. I extracted the jar and observed that there was another manifest file, not the one I had specified while creating artifact.

When I open manifest in IDE, it displays everything right but after creating jar I get a whole new manifest file.

Manifest-Version: 1.0
Main-Class: YoutubeList

I tried every solution from other answers and still not getting it right. Why creating a simple jar is hell of a task in Intellij, it was supposed to help developers!

Edited

And sometimes it does not include .class files in Jar which results in could not found or load class

mallaudin
  • 4,744
  • 3
  • 36
  • 68

10 Answers10

108

I was stucked with the same problem with maven build. When you are creating the artifact from project structure settings (ctrl+alt+shift+S), you have to change manifest directory:

<project folder>\src\main\java 

change java to resources

<project folder>\src\main\resources

I have also used the option extract to the target JAR, and it's working well.

EDIT

You can find a detailed step-by-step, an other solutions here: https://stackoverflow.com/a/45303637/2640826

Zsolt Tolvaly
  • 3,528
  • 4
  • 26
  • 25
  • I had to tweak jar task in gradle. – mallaudin Oct 27 '16 at 11:06
  • it works for me perfectly. However not able to find the configuration to change the path so From the windows explorar i chaged it.. works Fine.. – viralpatel Dec 23 '16 at 09:32
  • 6
    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. – Abhishek Saini Nov 29 '17 at 05:32
  • 2
    Copied by Jetbrains forum: "Apparently, IntelliJ does not read the manifest file that is located in the src/main/java folder! Working Solution: As suggested in the ticket, we moved the /META-INF folder to src/main/resources and rebuild the artifact." – lucasddaniel Jun 11 '18 at 19:44
  • Thank you, this solved my problem... I just created a new artifact and changed the folder name before clicking OK. – Michael Sims Oct 23 '18 at 17:47
  • 1
    IntellijIdea 2018.2 only @Abhishek Saini solution worked. – Dr Deo Dec 01 '18 at 07:20
  • I had created a Gradle project using IntelliJ IDEA and official tutorials for generating JAR file didn't help me, I was getting a message "no main manifest attribute" when executing the generated JAR in cmd. This solution worked perfectly for me. Thank you! – Salivan Mar 16 '20 at 10:47
  • For Mac users, `Cmd` + `;` opens project settings – abdul rashid Jun 18 '20 at 09:41
  • This worked for me. Interesting solution. Never would have thought to change that. Thanks! – armitus Dec 27 '20 at 20:17
  • It worker for me. Thank you! – phancuongviet Oct 19 '21 at 07:21
  • intellij 22.1.4 version also works. went through a crazy route to find this... – jake wong Sep 16 '22 at 16:53
9

I spent a few days to resolve it. My solution: I loaded a project that present in this answer. Then I compared and corrected settings of the loaded project and my own project. I compared/corrected:

  • Run/Debug Configurations
  • MANIFEST.MF
  • in Progect Structure settings: Project, Modules (mark what is sources, resources and etc), Artifacts.

In the end, I placed META-INF in resources directory.

Maybe i did excess actions, but it worked for me :)

P.S. also need to choose "Inherit project compile output path" in Progect Structure settings -> Modules -> Path

enter image description here

Maksim Ryabovol
  • 361
  • 3
  • 8
  • 1
    I was following the steps in https://www.jetbrains.com/help/idea/creating-and-running-your-first-java-application.html and stuck at this issue. I just moved the /META-INF/* folder from /java to /resources directory. This worked for me! – Kumar Aug 01 '19 at 09:33
8

If using Maven, Ensure your pom.xml has the main class referenced and fully qualified, similar to:

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

(... of course the version number of the plugin may be different).

The main class being not fully qualified, is what leads people to suggest moving the manifest to another location (in order to satisfy the reference locally).

Jool
  • 1,706
  • 16
  • 14
7

Putting the META-INF folder in */resources can do, but is not the way: Intellij just puts all under main/resources or test/resources in the root level of the generated jar, that's why it works; but in a Java project, we usually put them under project root, which is the same level as src. Putting them under "resources" is breaking the convention, besides, they are not resource files.

Make sure you:

  • use the existent MANIFEST.MF file at project root;
  • the main class is correct
  • ticked the Include in Project build under "Project structure" -> "Artifacts" panel
  • have META-INF folder listed in the files to include in the jar, apart from "project compiled output", in the Output Layout tab
  • the generated file type is JAR, at right top corner

And then, save the settings, build again, and enter "Build" menu to "Build Artifacts..", and "build" the JAR. Check the generated jar again, run with java -jar.

enter image description here

WesternGun
  • 11,303
  • 6
  • 88
  • 157
3

Actually I solved it by adding the following lines in build.gradle

jar {
    manifest {
        attributes 'Main-Class': 'class name'
    }
    from {
        configurations.compile.collect {
            it.isDirectory()? it: zipTree(it)
        }
    }
}
mallaudin
  • 4,744
  • 3
  • 36
  • 68
3

For Idea Intellij 2020:

  1. Remove obfuscation if presents

  2. delete/(move somewhere outside the project) META-INF folder in explorer

  3. remove artifacts in project structure--> artifacts--> '-'

  4. rebuild the project

  5. add artifacts again to project structure--> artifacts--> '+'

  6. rebuild the project

  7. Build-->Build artifacts..

Now the new produced jar should work

CodeToLife
  • 3,672
  • 2
  • 41
  • 29
1

Under File / Project structure / Artifacts you can specify how your jar is to be built. You can chose either to use an existing file as your manifest, or create a new one, in which case you specify main class and class path...

Per Huss
  • 4,755
  • 12
  • 29
  • 2
    I did. I specified main class but resulting jar does not contain that manifest file. – mallaudin Jun 12 '16 at 16:34
  • That's the way it's done... Check again that you are building the correct jar, check the settings for it again, perhaps do a clean. Otherwise, I'm out of ideas... – Per Huss Jun 12 '16 at 16:43
  • I read somewhere, if one of your dependent libraries aren't signed, you can't decompress and compress them in the same jar. Try with the other option: "copy to the output directory and link via manifest". By the way, i'm using Intellij IDEA v2016.2.5 – Zsolt Tolvaly Oct 27 '16 at 11:15
0

In my case I had some dependencies added in my jar, which itself generated a Manifest. I didn't get it to work straight out of Intellij. However, what you can do is open the jar file with a zip program and add the Main class yourself and then copy it back into the jar:

enter image description here

JoschJava
  • 1,152
  • 12
  • 20
0

In IntelliJ Idea 2020

Follow the instructions in this video.

When you get to the Artifact creation point (Ctrl + Shift + Alt + S) change the location of the manifest as shown here. (\src\main\resources)

enter image description here

Adrian Smith
  • 1,013
  • 1
  • 13
  • 21
-1

I found a different solution to the ones posted. In the IntelliJ Project Structure Dialog used to build the contents of the .jar file, I had to manually add the META-INF folder containing the Manifest file. Here is how it looks at the end in the project structure dialog box. If you don't see the folder in the list of jar contents, then the manifest file is not part of the jar file.

This is part of the Project Structure Dialog box

The way I include the folder manually is to click the plus sign, select the Directory Content option and then navigate to the META-INF folder.

J.E.Tkaczyk
  • 557
  • 1
  • 8
  • 19