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