12

I am using Intellij IDEA 12.0.4 on MAC OS X 10.8.3 to create a jar file.

I go to File -> Project Structure -> Artifacts -> + -> Jar -> From modules with dependencies -> Selected Main Class after browsing -> selected extract to the target jar -> Directory for META-INF automatically gets populated -> OK -> Apply -> OK -> Build -> Build Artifacts -> Build

Now If I try to run jar using:

java -jar path_to_jar/jar_file

I get this error

no main manifest attribute, in path_to_jar/jar_file
$ java -version
java version "1.7.0_17"
Java(TM) SE Runtime Environment (build 1.7.0_17-b02)
Java HotSpot(TM) 64-Bit Server VM (build 23.7-b01, mixed mode)

Any pointers, what I am doing wrong?

Kebab Programmer
  • 1,213
  • 2
  • 21
  • 36
user2213182
  • 131
  • 1
  • 1
  • 4
  • 1
    Take a look on similar questions http://stackoverflow.com/questions/11166633/main-class-not-found-intellij-building-a-jar and http://stackoverflow.com/questions/1082580/how-to-build-jars-from-intellij-properly. – Mihai8 Mar 30 '13 at 22:20

4 Answers4

21

To fix this issue do two things:

move META-INF in to src/main/resources

then: file-> open module settings

choose artifacts, remove your current one and create a new one, but this time, instead of choosing "extract to the target jar", choose "copy to the output directory and link via manifest"

for the path to META-INF, put the path of the resources dir above that you copied META-INF in to..

now build your artifact

im terminal, go to dir where jars have been produced and to run do:

java -jar yourjarfile.jar

have fun

felbus
  • 2,639
  • 2
  • 24
  • 30
4

In my case, it was because IntelliJ was extracting all the dependencies into the output jar, which ended up overwriting the /META-INF/MANIFEST.MF file with something from one of my included dependencies.

I had to open up the jar and copy in the real MANIFEST.MF from the location indicated in the "Manifest File" property that is automatically set in that artifacts page under Project Structure".

Kebab Programmer
  • 1,213
  • 2
  • 21
  • 36
Josh
  • 173
  • 3
  • 7
  • Although this may work surely it's better not to have to open the JAR and hack it about after generating? I followed the instructions from Felbus which fixed it for me without having to hack the jar around – Cookalino Feb 18 '14 at 14:57
0

You need to choose the "main" class for your jar in order to make it executable (i.e. you should point in IDEA on the class which contains public static void main(String[] args)

Shmil The Cat
  • 4,548
  • 2
  • 28
  • 37
0

In my case, I choose File -> Project Structure -> Artifacts -> + -> Jar -> From modules with dependencies -> Selected Main Class after browsing -> copy to the output directoryand link via manifest(extract to the target Jar not working for me) -> Select your project resources folder after browsing for /META-INF/MANIFEST.MF-> OK -> Apply -> OK -> Build -> Build Artifacts -> Build

My Idea version is IntelliJ IDEA 2018.2 (Ultimate version) I read from here. It works for me.