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

5
  • If you have a Maven or Gradle project, the JAR is already created for you when you build and package the project. If you aren't using dependency management, you probably should Jun 12, 2016 at 16:25
  • "When you run gradle build, Gradle will compile and test your code, and create a JAR file containing your main classes and resources" - docs.gradle.org/current/userguide/tutorial_java_projects.html Jun 12, 2016 at 16:29
  • I didn't say automatically, I said when you build the project Jun 12, 2016 at 16:30
  • Yeah, I mean when you build the project.
    – mallaudin
    Jun 12, 2016 at 16:33
  • Check "include in project build". See my answer below.
    – WesternGun
    Jan 23, 2019 at 14:53

10 Answers 10

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

11
  • I had to tweak jar task in gradle.
    – mallaudin
    Oct 27, 2016 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, 2016 at 9: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. Nov 29, 2017 at 5: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." Jun 11, 2018 at 19:44
  • 1
    IntellijIdea 2018.2 only @Abhishek Saini solution worked.
    – Dr Deo
    Dec 1, 2018 at 7:20
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

1
  • 1
    I was following the steps in jetbrains.com/help/idea/… and stuck at this issue. I just moved the /META-INF/* folder from /java to /resources directory. This worked for me!
    – Kumar
    Aug 1, 2019 at 9: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).

3
  • This did not work for me... am I to understand that it's done differently depending on using maven, gradle, ...?
    – TheGeeko61
    Jul 25, 2018 at 19:22
  • Didn't help for me too
    – Bharat
    Nov 12, 2019 at 4:52
  • Now another error occurs: Error: Could not find or load main class
    – Armine
    Nov 18, 2020 at 2:02
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

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

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

3
  • 2
    I did. I specified main class but resulting jar does not contain that manifest file.
    – mallaudin
    Jun 12, 2016 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, 2016 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 Oct 27, 2016 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

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

1
  • Doesn't work for me, are you using Gradle? Dec 29, 2020 at 21:57
-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.

0

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