user2213182
user2213182

Reputation: 131

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?

Upvotes: 12

Views: 30815

Answers (4)

Josh
Josh

Reputation: 183

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

Upvotes: 4

Chong Liu
Chong Liu

Reputation: 1

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.

Upvotes: 0

felbus
felbus

Reputation: 2669

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

Upvotes: 21

Shmil The Cat
Shmil The Cat

Reputation: 4668

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)

Upvotes: 0

Related Questions