Reputation: 4800
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
Upvotes: 67
Views: 106428
Reputation: 1073
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)
Upvotes: 0
Reputation: 4171
For Idea Intellij 2020:
Remove obfuscation if presents
delete/(move somewhere outside the project) META-INF
folder in explorer
remove artifacts in project structure--> artifacts--> '-'
rebuild the project
add artifacts again to project structure--> artifacts--> '+'
rebuild the project
Build-->Build artifacts..
Now the new produced jar should work
Upvotes: 3
Reputation: 1785
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).
Upvotes: 9
Reputation: 1212
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:
Upvotes: 1
Reputation: 12787
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:
MANIFEST.MF
file at project root;Include in Project build
under "Project structure" -> "Artifacts" panelMETA-INF
folder listed in the files to include in the jar, apart from "project compiled output", in the Output Layout
tabAnd 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
.
Upvotes: 7
Reputation: 4800
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)
}
}
}
Upvotes: 3
Reputation: 3616
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
Upvotes: 109
Reputation: 587
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.
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.
Upvotes: -1
Reputation: 361
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:
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
Upvotes: 9
Reputation: 5105
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...
Upvotes: 1