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:
Comments below can no longer be edited.
duto says:
August 28, 2010The 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
Michael says:
October 3, 2010Having 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?
bear says:
July 5, 2011in 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
Denis says:
December 3, 2011Please tell me where is created jar-file ??? I can not find it in the project folder.
Viewer says:
August 6, 2019In your target folder
Nikolay Chashnikov says:
December 5, 2011Denis,
path to the jar file is specified in ‘Output directory’ in the artifact settings. By default it is under /out/artifacts directory.
bipul says:
December 29, 2011I 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
Nikolay Chashnikov says:
December 30, 2011bipul,
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.
Pepe says:
January 17, 2012Hi, sorry for my english. How can i exclude a single file from the jar created in that way?
Nikolay Chashnikov says:
January 23, 2012Pepe,
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.
Jon says:
June 20, 2014I too would need this function. Things like config files, which are part of the project, but should be outside of the jar file.
Carfield Yim says:
August 26, 2016I also need this function, if anyone find the solution?
Peter says:
June 23, 2012When 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?
Edy Armendariz says:
July 31, 2012Hello 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?
Jon Wu says:
August 20, 2012Solution 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.
Jakob says:
September 28, 2016Hi Jon Wu, all,
thank you for all your efforts.
In a similar problem involving singed third-part .jars, I solved the problem by removing MANIFEST.SF and MANIFEST.RSA entries from the META-INF directory in the .jar generated by IntelliJ. I’ll give the automatic post-processing ant task a chance, but am in hurry and it is a one-of job.
Thanks again
Danish Amjad says:
May 16, 2017This is due to the signed dependencies, please check this answer:
http://stackoverflow.com/questions/41746177/how-do-i-create-a-runnable-jar-in-intellij-as-i-would-in-eclipse/43855741#43855741
Jon Wu says:
August 20, 2012Whoops, can’t paste XML into here. Here’s the ant build target: https://gist.github.com/3406283
Jeff says:
October 15, 2012I 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?
jack says:
June 10, 2013I am getting the error “Failed To load Main-Class manifest attribute from jar”.
Dinuka says:
June 24, 2013Thanx a lot for sharing your knowledge.. this article has saved my time…!!
Josh says:
August 16, 2013java 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…
Josh says:
August 16, 2013To 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:
April 6, 2017Thank 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?
to get your ex back and says:
December 8, 2013I 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!
JarConfused says:
January 1, 2014I’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?
Nikolay Chashnikov says:
January 13, 2014IDEA 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.
IntelliJ Novice says:
March 1, 2014I 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
Nikolay Chashnikov says:
March 3, 2014IntelliJ 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.
Hadeser says:
January 6, 2015please update blog !!!!
Lemurantin says:
February 1, 2017Hey 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!
ChipOfTheOldBlock says:
January 12, 2015Is 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)
Peter says:
February 18, 2015> 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 =)
Henry says:
June 25, 2015the 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” ?
Nikhil says:
September 24, 2015I 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.
Roy Truelove says:
September 25, 2015I’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.
CapitanShinChan says:
November 3, 2015How 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:
December 3, 2015Hello,
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.
Terry says:
November 25, 2015I’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?
Nikolay Chashnikov says:
December 3, 2015Hello Terry,
what do you mean by “this issue”? Could you please file a bug in our issue tracker and provide more details than “can’t get a simple project .jar file”.
Maxim says:
December 16, 2015Hi 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:)
Lauri Warsen says:
August 24, 2016Hi!
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:
August 25, 2016Hi 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.
Noah J Anderson says:
December 7, 2016Is it possible to bundle jre into the jar?
Nikolay Chashnikov says:
December 8, 2016Hello 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.
Thakshila Dilrukshi says:
December 11, 2016Hi!,
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
Sophie says:
January 9, 2017Hi,
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!
harraz says:
March 24, 2017I 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?
Nikolay Chashnikov says:
March 27, 2017Hello harraz,
new File(“data”) refers to a directory ‘data’ in the working directory of your process, it cannot access files inside a JAR. To read files from the JAR you either need to call Class.getResourceAsStream (https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getResourceAsStream-java.lang.String-) method or unpack the JAR.
himanshu says:
June 19, 2017Is 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:
June 20, 2017Yes, 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.
rshr says:
October 17, 2017When 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?
Jian Lan says:
January 14, 2018Does anyone think that if there is a new option “From Module without Dependencies” when creating JAR is a good idea? I don’t want to edit the “Output Layout” every time when I create a new artifact that I don’t want to include dependencies.
If use the current “From Module with Dependencies” without editing the “Output Layout”, I don’t know how can the jlink work with the generated artifacts (when I do this, there is an Error message).
Jon K says:
January 13, 2019The solution suggested by Lemurantin of moving/creating the META-INF directory , containing the manifest file, into the .\main\resources folder worked for me in Intellij 18.3
Jon K says:
January 30, 2019Update to my previous comment .
I can no longer get Intelli 2018.3.4 to build a jar file. The manifest file in the created jar contains:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.6.5
Created-By: 1.4.2_12-b03 (Sun Microsystems Inc.)
which has come from somewhere, where I have no idea.
The generated Manifest file, which is the one I want in the jar contains:
Manifest-Version: 1.0
Main-Class: GroovyLauncher
Nikolay Chashnikov says:
January 31, 2019Hello, Jon,
Most probably this manifest comes from one of libraries unpacked into your artifact. Please create an issue in our tracker (https://youtrack.jetbrains.com/issues/IDEA) and attach the artifact configuration file (from .idea/artifacts directory) to it so we can check this.
If this is the case, you can fix the problem by changing order of elements in the artifact configuration to ensure that proper manifest will take precedence, see our help for details (https://www.jetbrains.com/help/idea/output-layout-tab.html).
Jon K says:
January 30, 2019I did find a very hacky way of modify the generated Manifest.mf file
Using 7zip I copied the required Main-Class specification into the one included by the build process and the jar file then worked!
Siem says:
June 2, 2020Kotlin looks great, but this thread is extremely worrying. I went through the education stuff and learned IntelliJ IDEA, but I’m afraid I wasted my time.
I know nothing about manifests, wrappers, whatever. I want a simple solution to write independent programs. For Windows as a start, but also for other platforms.
Where is the button to create a simple jar from a kt file?
My HelloWorld works, but I have no jar file.