Quickly create Jar artifact for application

Since the last EAP build IntelliJ IDEA X provides an action to quickly create a single Jar artifact containing your modules with all dependencies. Just press ‘+’ button in the Project Structure dialog and select the appropriate item:

IntelliJ IDEA shows a dialog allowing you to customize the artifact:

After that you can build the Jar file using Build | Build Artifact menu item.

Note that by default all libraries are extracted to the target Jar. It became possible with addition of the new Extracted Directory element. Using such element you can extract a directory from a Jar file and place it into the output of your artifact:

This entry was posted in New Features and tagged . Bookmark the permalink.

23 Responses to Quickly create Jar artifact for application

  1. duto says:

    The path of manifest file is not update when we add or remove librairies or change directory of librairies (move) . It’s too domage :s

    Duto

  2. Michael says:

    Having issue runnin JAR packaged via IDE. Get the following error

    “Exception in thread “main” java.lang.SecurityException: Invalid signature file”
    Any ideas what could be wrong?

  3. bear says:

    in previous version of intellij it was easay to build a jar: now it is extremely non intuitive!
    the main problem is that I build a different jar for each module without dependencies!!!
    how do you do that?
    not obvious

  4. Denis says:

    Please tell me where is created jar-file ??? I can not find it in the project folder.

  5. Nikolay Chashnikov says:

    Denis,
    path to the jar file is specified in ‘Output directory’ in the artifact settings. By default it is under /out/artifacts directory.

  6. bipul says:

    I am trying to create jar file. My project contains external csv files outside src folder that is essential for the project. But when running the jar file it seems as though they are not being involved.

    Any Idea

  7. Nikolay Chashnikov says:

    bipul,
    files outside source roots aren’t included into jar file by default. However you can add them by hand: open the artifact settings in Project Structure dialog, press ‘+’ button under ‘Output Layout’ tab, select ‘File’ item and choose the desired files.

  8. Pepe says:

    Hi, sorry for my english. How can i exclude a single file from the jar created in that way?

  9. Nikolay Chashnikov says:

    Pepe,
    it isn’t possible now. However you can move that file to a separate module. This way you also ensure that other files don’t depend on the excluded file.

  10. Peter says:

    When I try to run the jar executable, I get the ‘Could not find main class. Program will exit’ error. I used the browse tool to locate the Main Class, in Project Structure>Artifacts, but nothing I have tried putting in the Class Path text field seems to properly locate the class file (even leaving it empty).

    The main class is in the first package folder in src, so src/package1/MainProgram.
    The Main Class field reads package1.MainProgram
    I have tried relative and absolute path names for the Class Path, but nothing works. Currently it is: out/production/ProjectName/package1/

    What am I doing wrong?

  11. Edy Armendariz says:

    Hello Team,

    I am also receiving this error when running the jar from the Terminal.

    “Exception in thread “main” java.lang.SecurityException: Invalid signature file digest for Manifest main attributes”

    What am I doing wrong?

  12. Jon Wu says:

    Solution for “java.lang.SecurityException: Invalid signature file digest for Manifest main attributes”

    The problem is that one of the jars you extracted was signed. You could link the jars up instead of extracting them, but that’s not nice if you just want one jar file to run and share. I solved this with an ant post-processing task like this. Just change output.jar to the right name for your artifact.

  13. Jon Wu says:

    Whoops, can’t paste XML into here. Here’s the ant build target: https://gist.github.com/3406283

  14. Jeff says:

    I am now packaging my app to a jar and this is working great, thanks! Unfortunately I have a network copy step in my deployment to test servers and I would rather not send the entire groovy distribution every time I send a new iteration out. Is there a recommended way to send out an executable “my code only” jar which references the all in one?

  15. jack says:

    I am getting the error “Failed To load Main-Class manifest attribute from jar”.

  16. Dinuka says:

    Thanx a lot for sharing your knowledge.. this article has saved my time…!!

  17. Josh says:

    java test.jar “Error: Could not find or load main class test.jar”
    java -jar test.jar “no main manifest attribute, in test.jar”

    Runs just fine from the IDE. All that confusion just to build a non-functional “executable” jar file…

  18. Josh says:

    To all those having problems, I finally figured out why IntelliJ can fail at making a working executable jar:

    In my case, it was because IntelliJ was extracting all my dependencies into the output jar (good), which ended up overwritting the /META-INF/MANIFEST.MF file with something from one of my dependencies (bad). 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”.

  19. I like the valuable info you provide in your articles. I’ll bookmark your weblog and check again here frequently.
    I am quite sure I’ll learn many new stuff right here!
    Good luck for the next!

  20. JarConfused says:

    I’ve been Googling for hours, on this. All Intellij instructions for generating a jar, are dedicated to generating an artifact-style executable jar. I know how to do that, and it’s worked fine, aside from sometimes having to manually remove Kitfox.* files.

    But how in the world, can you coax Intellij to generate a jar WITHOUT a main class? It’s easy to do from the command line, but it’s not clear how to do it, within the Intellij project.

    All the class files in the project are solid, they compiled fine. But how in the world, can we generate just a basic jar, from within Intellij without a main attribute in the manifest?

  21. Nikolay Chashnikov says:

    IDEA doesn’t generate main-class attribute itself so if you get it in your jar file it means that you have manifest.mf file with this attribute in your sources.

  22. IntelliJ Novice says:

    I have multiple files (Java classes with main) in my package. I want to create separate jar files for each java class. Right now I can create a jar with one name – main project name. Please help. Thanks

  23. Nikolay Chashnikov says:

    IntelliJ Novice,
    if you need to create separate jars for several java classes it would be better to move these classes to separate modules. After that you can use ‘Jar -> From module with dependencies’ action to create separate artifacts for these modules.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>