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.

51 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”.

    • Anthony Lord says:

      Thank you, I still had this issue just now and your solution works fine.

      How do we fix that so we dont have to manually edit every JAR though?

  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.

  24. Hadeser says:

    please update blog !!!!

    • Lemurantin says:

      Hey guys, after hours of researches I finally found why the generated jar wasn’t working, and I guess you will probably encounter the same mistake, so let me help you with this comment.

      When you are generating your jar the way this tutorial recommands you to do it, your manifest file is not copied inside. If you want it to be added to your jar, simply move your META-INF directory (containing your manifest file) inside the resources directory. Build again and your jar will be runnable!

  25. ChipOfTheOldBlock says:

    Is there a way to create a Jar file that includes just a couple of classes (selected by user). Usually, we need to do this to provide fixes (instead of releasing the whole app)

  26. Peter says:

    > 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

    That happened to me as well. I tried navigating to the generated manifest file in project and ‘Package file’ it and also changed the order of packages in jar (you need to disable sorting for that) and it seems disappeared =)

  27. Henry says:

    the intellij make the lib.jar to class file,not the original jar lib,how can we do it as eclipse “Extract required libraries into generated jar” ?

  28. Nikhil says:

    I am getting this error while running the jar created using intellij
    “Exception in thread “main” java.lang.SecurityException: Invalid signature file digest for Manifest main attributes”
    But when a jar for the same code/project is generated using eclipse it works fine.

  29. Roy Truelove says:

    I’ve given up on getting an executable jar w/ Intellij. Two issues, one already mentioned:

    1) Intellij creates a manifest for you but it gets overridden by the manifests in the dependent libraries. You should create your own manifest and put it in src/main/**resources** and not src/main/java

    2) It doesn’t handle this: https://maven.apache.org/plugins/maven-shade-plugin/examples/resource-transformers.html#AppendingTransformer

    Frustrating.

  30. CapitanShinChan says:

    How can I generate a Jar from .java files, if I don’g have a main class?

    I don’t want to run this jar separately, it’s going to be load in another program.

    This seems a pretty basic operation but I can’t get it done in IntelliJ Idea (using version 15).

    • Nikolay Chashnikov says:

      Hello,
      if you want to create a jar without main class you can just use the action described in this blog post (JAR -> From Module with Dependencies), though the UI is slightly different in the latest version (the blog post is 5 years old). ‘Main Class’ field is optional and you can leave it empty. If it doesn’t work for you please file a bug in our issue tracker.

  31. Terry says:

    I’m in the process of moving from NetBeans to IntelliJ, because it is used by those making several important projects I’m involved with. It is beautiful, but I’m amazed that I still can’t get a simple project .jar file out of it! I want to run under Spark, and I really don’t want to have to import it into NetBeans just to get a reasonable damn .jar

    Looking at the comments above, for literally years, I can’t image why the developers have not addressed this issue. Does this work in the “professional” version?

  32. Maxim says:

    Hi there! For all who have a problem “no main manifest attribute, in “. All you need is to add to your Artifact a folder with name “META-INF” with content of a folder with correct MANIFEST.MF file and locate this folder at the top of jar content. So for case of Idea 13.1.3 all you need is to open Project Structure -> Artifacts -> activate your artifact -> in window “Output Layout” click on “Create Directory”, name it “META-INF”, then select it and click on “+” and choose “Directory content” – navigate to your directory with correct manifest file. And then just disable sorting and “Move up” this new “META-INF” directory at the top of files hierarhy. That is all, hope it will save your time:)

  33. Lauri Warsen says:

    Hi!
    I’m working on a game in java and there appears to be a problem when trying to run an jar artifact built by the IDE. Building the artifact seems to work fine, but when trying to run the artifact nothing happens at all. First it opened a windows with a grey background and in attempt to fix that I made a new project and copied all the files over. Nothing seems to work and I can’t find any people who’ve had this exact problem either.

    I’m using the latest community version
    Thanks!

    • Nikolay Chashnikov says:

      Hi Lauri,
      looks like the problem is in your application, not in JAR artifact. Does it work if you run the application via ‘Application’ run configuration, without creating JAR file?
      BTW it’s better to report problems directly to our issue tracker.

  34. Noah J Anderson says:

    Is it possible to bundle jre into the jar?

    • Nikolay Chashnikov says:

      Hello Noah,
      what do you mean by ‘bundle jre’? You can use ‘+’ -> ‘Directory Content’ in ‘Layout Tree’ to add a directory containing JRE into an artifact near to a JAR file and write a script which will run the JAR using that JRE.

  35. Thakshila Dilrukshi says:

    Hi!,
    When i’m trying to create the artifact i’m getting below error:
    ‘META-INF/MANIFEST.MF’ already exists in VFS’. I couldn’t find a way to resolve this. Can you please help me on that

  36. Sophie says:

    Hi,

    I am trying to create a jar file for a program, that uses two predefined txt files as database. When the jar file is in the project folder, it works fine. However, when I move it to any other folder in the computer, it doesn’t read the files. I have added the folder they are in as Source and I have added these two txt files in the Output Layout of the artifact. Both of these didn’t work. Can you please give me some advice?

    Thanks in advance!

  37. harraz says:

    I added a directory called /data to the jar artifact, and I can see that it’s part of the jar now. The problem is in the IDE i used to be able to access that directory from the code like this

    File[] files = new File(“data”).listFiles();

    But when I run the application using java -jar I get a null pointer exception, and I think because it can’t find the location anymore. All I want to accomplish is to list the file names under /data directory and put them in an array list. It works when I run from IDE but not when I run from command line

    Any ideas how to fix this?

  38. himanshu says:

    Is their any way to export only one or set of specific files to jar and not the entire package ?
    We are working on a big project so the files that are changed we first need to be exported out and deployed as a patch .

    • Nikolay Chashnikov says:

      Yes, it’s possible to include individual files into a JAR. You need to press ‘+’ button in ‘Output Layout’ tree in the artifact editor and choose ‘File’ to add a single file or ‘Directory Content’ to add all files from some directory into the JAR.

  39. rshr says:

    When I build my jar file the manifest file doesn’t have the class-path and main-class properties specified in it, even though I have it specified in the project structure manifest properties section. Any ideas as to what I am doing wrong?

Leave a Reply

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