Creating Jar with Intellij 2016 - No main manifest attribute

JavaIntellij IdeaJar

Java Problem Overview


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

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

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

Solution 3 - Java

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

Solution 4 - Java

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

Solution 5 - Java

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

Solution 6 - Java

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

Solution 7 - Java

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

Solution 8 - Java

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

Solution 9 - Java

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

Solution 10 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmallaudinView Question on Stackoverflow
Solution 1 - JavaZsolt TolvalyView Answer on Stackoverflow
Solution 2 - JavaMaksim RyabovolView Answer on Stackoverflow
Solution 3 - JavaJoolView Answer on Stackoverflow
Solution 4 - JavaWesternGunView Answer on Stackoverflow
Solution 5 - JavamallaudinView Answer on Stackoverflow
Solution 6 - JavaCodeToLifeView Answer on Stackoverflow
Solution 7 - JavaPer HussView Answer on Stackoverflow
Solution 8 - JavaJoschJavaView Answer on Stackoverflow
Solution 9 - JavaAdrian SmithView Answer on Stackoverflow
Solution 10 - JavaJ.E.TkaczykView Answer on Stackoverflow