297

I was using IntelliJ IDEA IDE. I want to create a JAR file from Java compiled class files. But I didn't find a command or file.

How to create a JAR file (like it is done in Eclipse)?

informatik01
  • 16,038
  • 10
  • 74
  • 104
sungkwangsong
  • 5,575
  • 4
  • 26
  • 18
  • I couldn't do it via Intellij, so I found [this](https://github.com/libgdx/libgdx/wiki/Gradle-on-the-Commandline#packaging-for-the-desktop) and it is super easy to generate the package via ./gradlew desktop:dist, I know it is not related with the question, but it really helped me in the task of creating the jar file. – igrossiter Sep 24 '14 at 12:56
  • 3
    please mark the answer that helped you the most. It help readers identify the right/useful answer :) – Matécsa Andrea May 27 '20 at 09:11

3 Answers3

440

For Intellij IDEA version 11.0.2

File | Project Structure | Artifacts then you should press alt+insert or click the plus icon and create new artifact choose --> jar --> From modules with dependencies.

Next goto Build | Build artifacts --> choose your artifact.

source: http://blogs.jetbrains.com/idea/2010/08/quickly-create-jar-artifact/

Ben
  • 6,107
  • 6
  • 29
  • 40
  • 23
    For anyone coming here that can't get it work. It worked for me when I stored the .mf file in the root directory and **not** in the /src/ folder. – Christophe De Troyer Aug 24 '14 at 14:12
  • 10
    It says cant load the main class.. :( – Ankur Bhatia May 18 '16 at 20:02
  • 1
    I always get "error: Class names, 'myStuff.jar', are only accepted if annotation processing is explicitly requested 1 error" when I run the jar – gimba Nov 09 '16 at 11:44
  • 2
    I can't tell if it works or not, where the file itself ? I need t copy the jar file but cannot find it (I am working on Ubutnu) – McLan Oct 31 '17 at 11:13
  • 1
    @McLan I have Ubuntu too and if you do everything like he said, you will find a new folder in your project root named 'out'. Just open all the folders inside of it and at the end you will find your .jar file. – Matécsa Andrea May 27 '20 at 09:08
  • I managed to create the jar file but for some reason when I am trying to extract the jar file I am getting currupted error. – Ali Dec 27 '20 at 19:28
52

You didn't specify your IDEA version. Before 9.0 use Build | Build Jars, in IDEA 9.0 use Project Structure | Artifacts.

CrazyCoder
  • 389,263
  • 172
  • 990
  • 904
  • 8
    Well here's how to do it with Intellij 10 is http://blogs.jetbrains.com/idea/2010/08/quickly-create-jar-artifact/ – simao Feb 04 '11 at 18:02
  • I am trying to build jar but i got error failed to load main-class manifest attribute from jar file. I have included external library also. Why the error comes – muthu Jun 10 '13 at 12:59
  • I get this also, Muthu. The MANIFEST.MF created by intellij/maven does not have a populated class path in my case. I simply use "mvm -e package" from the command line instead. – ctpenrose Sep 12 '14 at 21:54
  • @ctpenrose `mvm -e package` where setup ? command tool or intellij IDE ? – zhaoyou Jan 05 '15 at 08:32
  • @zhaoyou he meant `mvn -e package`, and you're supposed to run it on command line within the root directory of the project. this will generate a `jar` inside the `target` folder. however, this is probably exactly what IntelliJ does itself. – phil294 Jan 20 '17 at 23:55
4

In intellij8 I was using a specific plugin "Jar Tool" that is configurable and allows to pack a JAR archive.

dawez
  • 2,714
  • 2
  • 29
  • 50