Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to run a jar file created using intellij 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?

like image 681
user2213182 Avatar asked Mar 30 '13 22:03

user2213182


2 Answers

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

like image 110
felbus Avatar answered Nov 03 '22 22:11

felbus


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

like image 4
Josh Avatar answered Nov 03 '22 23:11

Josh