How to build JARs from IntelliJ IDEA properly?
Asked Answered
H

24

666

I have a project that contains a single module, and some dependencies. I'd like to create a JAR, in a separate directory, that contains the compiled module. In addition, I would like to have the dependencies present beside my module.

No matter how I twist IntelliJ's "build JAR" process, the output of my module appears empty (besides a META-INF file).

Hanging answered 4/7, 2009 at 17:5 Comment(0)
W
799

Instructions:

File -> Project Structure -> Project Settings -> Artifacts -> Click + (plus sign) -> Jar -> From modules with dependencies...

Select a Main Class (the one with main() method) if you need to make the jar runnable.

Select Extract to the target Jar

Click OK

Click Apply/OK

The above sets the "skeleton" to where the jar will be saved to. To actually build and save it do the following:

Build -> Build Artifact -> Build

Try Extracting the .jar file from:

πŸ“¦ProjectName
 β”— πŸ“‚out
   β”— πŸ“‚artifacts
     β”— πŸ“‚ProjectName_jar
        β”— πŸ“œProjectName.jar

References:

Waltner answered 4/7, 2009 at 17:5 Comment(20)
It fails when some classes in your included jar are signed with RSA. – Natiha
Link is dead. Do you have a copy of this article? – Bemoan
This creates a jar file with the class files (and the structure) only. I'm using gradle. – Abbey
For me it worked on 2016.2.1, but you have to first check the mark "Build on make" in the menu where you set the output directory, then you have to Run the application in Intellij so it generates the jar – Kerril
how can generate Tomcat embedded jar? – Perfecto
@AsifShahzad see this answer for building jar files with signed dependencies: #41746677. – Dermott
This method still fails, the jar file doesn't execute – Shatter
Be reminded to choose the correct Main Class, while doing Create JAR from Modules. Otherwise the JAR file will never work as you expected. – Disorderly
It seems someone has really worked very hard to not make it work. It is such a shame it never works. I have tried at least 50 different answers and none of it worked. And its the same story every single time. Now I reserve a week to just output it as a jar as everyone in our company thinks its a genuine effort that needs to be go in producing an output jar. – Tynishatynwald
As many wrote before me, this is a simplification that sometime works. Please see @zsolt tolvali's answer below. It would be good from IntelliJ's perspective to make the JAR creation process a DEFAULT ACTIVITY when creating a new project, because very few people do not need it! – Desirable
I sleected class, but get error message: "not acceptable" – Accelerando
it is almost 2020, and we have self driving car almost and we still can not figure out how to create executable jar in Intellij – Hiramhirasuna
I can't thank you enough for this after literally wasting 2 hours of my life trying to figure out how to tell Maven that I want the dependencies to come with the generated jar. – Majewski
After trying 100 times to make a jar using intelliJ, I finally found a solution. Open Eclipse --> export as Jar. Done. – Levee
how are Java IDEs still so garbage... this is insane – Mcclure
@Hiramhirasuna The worst hour of my life I spend trying to figure out how to get an executable of a java project. – Prole
2022-03-03: I can say that this approach worked well with IntelliJ IDEA 2021.3.2 (Ultimate Edition) – Demmer
Thank you SO MUCH! I wish I could award extra points for this <3 – Gravettian
none of these solutions ever works – Spastic
my honest suggestion, in case of trying to build one using intellij, one can use the command line to build a jar. (assuming its a small jar and you are not using maven to get it done) – Ctesiphon
E
379

This is still an issue in 2017, I hope it will help somebody out there! I found 2 possibilities to create working jar-s under IntelliJ 2017.2

1. Creating artifact from IntelliJ:

  • Go to project structure:

File menu

  • Create a new artifact:

Create a new artifact

  • Select the main class, and be sure to change the manifest folder:

enter image description here

You have to change manifest directory:

<project folder>\src\main\java 

replace "java" with "resources"

<project folder>\src\main\resources

This is how it should look like:

correct way for new manifest

  • Then you choose the dependencies what you want to be packed IN your jar, or NEAR your jar file

  • To build your artifact go to build artifacts and choose "rebuild". It will create an "out" folder with your jar file and its dependencies.

enter image description here

2. Using maven-assembly-plugin

Add build section to the pom file

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <finalName>ServiceCreate</finalName>
                    <appendAssemblyId>false</appendAssemblyId>
                    <archive>
                        <manifest>
                            <mainClass>com.svt.optimoo.App</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
  • Create a new run/debug configuration:

Create a new run/debug configuration:

  • Choose application:

Choose application

  • Fill in the form
  • Add the "assembly:single" maven goal after build to be executed last

enter image description here

Final setup

  • Save it, then run

enter image description here

This procedure will create the jar file under the "target" folder

JAR file location

Elvyn answered 4/7, 2009 at 17:5 Comment(11)
change manifest directory to src/resources is the key point !!! thanks~ – Raasch
I was getting no main manifest attribute adopting above solutions. No one has mentioned changing the path to manifest. Thnx!! – Guacharo
how about source code in scala ? java is not what I'm using – Monkey
Instead of resources, you can also change the directory to the one above java, e.g. ...\src\main\java -> ...\src\main and it works. Jet Brains should really fix this default, it's still a problem in 2020.1.4. – Showpiece
@Showpiece I think they fixed it. The default is now ...\src\main\resourses and if you don't have a manifest file there, a file will be created automatically. – Somatoplasm
I am getting no main manifest attribute, but it is in there and I followed all the gradle steps perfectly? – Tuckerbag
@RoyCohen No. It's still \src\main\java , IDEA 2021.3 – Nitride
how to add version numbers in generated jar file, if lets say I updated the code and build artifacts, then each time its generating same jar file without any version number, how this version can be appended to the generated jar file – Corvin
What's the differences between these 2 methods? – Haase
The first method is configuring IntelliJ to build the artifact, the 2nd one is adding a build plugin to the pom file. – Elvyn
Run it with java -jar "ServiceCreate.jar" – Leucocyte
S
152

I recently had this problem and think these steps are easy to follow if any prior solution or link is missing detail.

How to create a .jar using IntelliJ IDEA 14.1.5:

  1. File > Save All.
  2. Run driver or class with main method.
  3. File > Project Structure.
  4. Select Tab "Artifacts".
  5. Click green plus button near top of window.
  6. Select JAR from Add drop down menu. Select "From modules with dependencies"
  7. Select main class.
  8. The radio button should be selecting "extract to the target JAR." Press OK.
  9. Check the box "Build on make"
  10. Press apply and OK.
  11. From the main menu, select the build dropdown.
  12. Select the option build artifacts.
Struthious answered 4/7, 2009 at 17:5 Comment(4)
did you miss something ? There is an option about module what should be that. In my case it's creating JAR but JVM through error due to security. – Filtration
i think that list is everything. You might have something with your java settings stopping it due to security. Does anyone else have thoughts on this? – Struthious
Followed the same and I have Error: Could not find or load main class JavaFundamentals.jar when running this jar file. Explained here: #40637290 – Knout
Does this 12 step process have to be taken every time I want to build a jar, or will IntelliJ remember these actions so you can use a shortcut to it? – Calctufa
K
51

For those that benefit from images as I do:

File -> Project Structure

enter image description here

enter image description here

enter image description here

enter image description here

Karyolymph answered 4/7, 2009 at 17:5 Comment(3)
@samuelowino i don't appreciate you saying that. Explain what "doesn't work at all". – Bradytelic
Hey @Karyolymph I was not checking the right jar file, apparently, Inteliji produced two jar files on different directories, apologies, my bad, your solution works, thanks – Shatter
@Karyolymph I tried the above steps. but i keep getting no main manifest attribute, in myFile-0.1.jar – Linebacker
H
37

Here are 2 examples with maven project, step by step:

Method 1: Build jar with maven and pom.xml

  1. File, new, project, maven
  2. create a package , create a java program inside that package enter image description here
  3. at pom.xml, add maven-jar-plugin.

        <plugin>
            <!-- Build an executable JAR -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.1.0</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>somePackage.sample</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    

screen capture: enter image description here 4. open maven project box by click on the search icon and type maven, enter image description here

  1. click on clean, then click on install enter image description here

  2. your jar file will show up inside the target folder enter image description here

  3. test your jar using java -jar

enter image description here

Method 2: Build jar with maven without pom.xml change

  1. File, new, project, maven
  2. create a package , create a java program inside that package (same as above)
  3. File -> Project Structure -> Project Settings -> Artifacts -> Click green plus sign -> Jar -> From modules with dependencies

Very important!

The MANIFEST.MF needs to be in your resources folder and the Main.Class needs to refer to {package}-{class-name-that-contains-main-class}

enter image description here enter image description here

  1. then click build on menu , build artifacts, build

enter image description here

  1. find the jar in the out folder

enter image description here

  1. run it :)

enter image description here

Holy answered 4/7, 2009 at 17:5 Comment(4)
this works. Can you explain why MANIFEST.MF needs to be in that directory? – Assuntaassur
This helps for me – Drynurse
This is very helpful! The second option worked best for me. Just a quick update: IntelliJ opens an extra wizard to create the manifest file. This will allow you to create it and place it in the right location without having to write anything. – Centennial
Thats very helpfull – Appreciation
O
7

When i use these solution this error coming:

java -jar xxxxx.jar

no main manifest attribute, in xxxxx.jar

and solution is:

You have to change manifest directory:

<project folder>\src\main\java 

change java to resources

<project folder>\src\main\resources
Oneirocritic answered 4/7, 2009 at 17:5 Comment(1)
I already have this, but still have a problem – Tuckerbag
I
6

It is probably little bit late, but I managed to solve it this way -> open with winrar and delete ECLIPSEF.RSA and ECLIPSEF.SF in META-INF folder, moreover put "Main-class: main_class_name" (without ".class") in MANIFEST.MF. Make sure that you pressed "Enter" twice after the last line, otherwise it won't work.

Install answered 4/7, 2009 at 17:5 Comment(2)
For signed dependencies, please have a look at this answer: #41746677 – Dermott
What an insane issue. Any idea why this happens or how to fix? This feels hacky – Floccus
S
5

I was trying to build a jar from a multi-module Kotlin app and was getting the notorious 'no main manifest attribute' error. Creating the manifest directory in src/main/resources didn't work either.

Instead, it works when creating it in src directly: src/META-INF/MANIFEST.MF.

enter image description here

Souvenir answered 4/7, 2009 at 17:5 Comment(1)
Hi, Thanks it's working fine. But now I have a new error that it's not finding the resources "java.desktop/javax.swing.ImageIcon java.lang.NullPointerException" – Auster
D
3

enter image description here

As the people above says, but I have to note one point. You have to check the checkbox:

Include in project build

Dualism answered 4/7, 2009 at 17:5 Comment(0)
S
2

Using

mvn clean package spring-boot:repackage

type above command inside IntelliJ Terminal. It generates a Target file with Executable Jar.

Sting answered 4/7, 2009 at 17:5 Comment(0)
S
2

With Maven you can use this plugin:

 <build>
    <plugins>

        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>[path you class main]</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase> 
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
Slue answered 4/7, 2009 at 17:5 Comment(0)
Y
1

My problem was this: I used Intellij IDEA (tried different versions) + Gradle. When I compiled the project and builded artifacf jar, as indicated in the above responses, I received an error - "no main manifest attrubute ..."

This solution worked for me (special thanks to Thilina Ashen Gamage (see above) for tip):

Excerpt - if you use external libraries and build a project through Project Settings - Artifacts - Add (+) - Jar - From modules with dependencies, then probably because of a program bug the META-INF folder with MANIFEST_MF file not including to jar. To avoid it create EMPTY jar file.

Project Settings - Artifacts - Add (+) - Jar - EMPTY JAR. META-INF folder will added to resources folder. Then select your main class. You will see following jar structure: note the presence of a folder META-INF Note the presence of a folder META-INF Then you can build your project and build artifacts. This solution worked for javaFX applications too.

Yod answered 4/7, 2009 at 17:5 Comment(0)
P
1

Here is the official answer of IntelliJ IDEA 2018.3 Help. I tried and It worked.

To build a JAR file from a module;

  1. On the main menu, choose Build | Build Artifact.

  2. From the drop-down list, select the desired artifact of the type JAR. The list shows all the artifacts configured for the current project. To have all the configured artifacts built, choose the Build all artifacts option.

Peak answered 4/7, 2009 at 17:5 Comment(0)
M
1

Idea 8.1.3

Jar is ok, since there is compiled output in 'output' directory (project/out/production//)

I guess, you have to run 'make' before building jar

for dependencies just check "show library" and choose what you want.

Masoretic answered 4/7, 2009 at 21:15 Comment(1)
Without gradle its working properly but when i build gradle based project in IntelliJIdea its show C:\Validate\out\artifacts\Validate_jar>java -jar Validate.jar Error: Could not find or load main class Validate Caused by: java.lang.ClassNotFoundException: Validate – Unravel
L
0

If you're using the new beta IntelliJ UI, the above solutions will require modification.

Currently, in the new beta IntelliJ UI, there is a bug where if you click on the "build" button in the build artifacts menu, nothing will happen.

However, if you use arrow keys to navigate to the "build" button, and then press "enter" on your keyboard, it will work.

or you can do the following:

You have to shift+shift type [Build Artifacts...] > then use arrow keys to navigate build artifact floating menu and hit enter.

Bug tracker: https://youtrack.jetbrains.com/issue/IDEA-316889/Build-artifacts-inactive-if-hamburger-menu-is-on

Hopefully this saves someone from the hour of pain I experienced trying to get my JAR file to be built.

Lenhart answered 4/7, 2009 at 17:5 Comment(0)
G
0

I tried most of the given answers, but in my case "out" folder (which contains jar file) was not getting created. So this did the job :

Build -> Build Artifacts -> (you will see the class name) -> Build

Now, "out" folder was created and jar file was in it.

Goad answered 4/7, 2009 at 17:5 Comment(0)
O
0

Use Gradle to build your project, then the jar is within the build file. Here is a Stack Overflow link on how to build .jar files with Gradle.

Java project with Gradle and building jar file in Intellij IDEA - how to?

Ozalid answered 4/7, 2009 at 17:5 Comment(0)
U
0

In case you are reading this answer because you are facing "resource file not found" error, try this:

  1. File -> Project Structure -> Artiface -> New, type is Other.
  2. Give it a nice name, and in the Output Layout, press Create Archive to create a new jar, and again, give it a nice name ;)
  3. Click on the jar you just added, add your compiled module output in the jar.
  4. Click on the jar again, in the lower pane, select your project path and add Manifest File then set correct Main Class and Class Path.
  5. Click Add Library Files, and select libraries you need (you can use Ctrl+A to select all).
  6. Build -> Build Artifact -> Clean & Build and see if the error is gone.
Urbane answered 4/7, 2009 at 17:5 Comment(0)
S
0

In case you are trying to build a jar with kotlin you need to create a src/main/java folder and use this folder as a location for the META-INF folder.

Sedlik answered 4/7, 2009 at 17:5 Comment(0)
H
0

Some of the other answers are useless because as soon as you re-import the IntelliJ IDEA project from the maven project, all changes will be lost.

The building of the jar needs to be triggered by a run/debug configuration, not by the project settings.

Jetbrains has a nice description of how you can accomplish this here:

https://www.jetbrains.com/help/idea/maven.html

Scroll down to the section called "Configuring triggers for Maven goals".

(The only disadvantage of their description is that their screenshots are in the default black-on-white color scheme instead of the super-awesome darcula theme. Ugh!)

So, basically, what you do is that you open the "Maven Projects" panel, you find the project of interest, (in your case, the project that builds your jar,) underneath it you find the maven goal that you want to execute, (usually the "package" goal creates jars,) you open up the context menu on it, (right-click on a Windows machine,) and there will be an "Execute before Run/Debug..." option that you can select and it will take you by the hand from there. Really easy.

Hydrous answered 4/7, 2009 at 17:5 Comment(0)
F
0

If you are using third party libraries with your project or if you have problems with creating MANIFEST.MF file properly, there can be conflicts when running JAR files generated using

File > Project Structure > Artifacts > '+' > JAR > From modules with dependencies > .....

method mentioned above.

Instead I suggest you to create an empty JAR and add all other elements to the output root manually. A wonderful blog article for this method can be found here: http://karthicraghupathi.com/2016/07/10/creating-an-executable-jar-in-intellij-idea/ I tried the steps mentioned there and everything worked fine for me!

Fractostratus answered 4/7, 2009 at 17:5 Comment(1)
Thanks, this helped me! There appears to be a bug in Intellij where the manifest of the project is overwritten in the JAR by a default manifest if you select "JAR" as the artifact type instead of "Other". – Booklover
W
0

You might want to take a look at Maven (http://maven.apache.org). You can use it either as the main build process for your application, or just to perform certain tasks through the Edit Configurations dialog. The process of creating a JAR of a module within Maven is fairly trivial, if you want it to include all the dependencies in a self-executable JAR that is trivial as well.

If you've never used Maven before then you want to read Better Builds With Maven.

War answered 5/2, 2010 at 9:52 Comment(5)
Maven is obviously overhead for a simple tasks like creating jar. Maybe Ant but not maven for sure. – Goldman
Link is dead (Oct 2014) – Maroney
@Maroney Thanks - updated to use the Maven team's recommendations. – War
If it's so trivial, why not include it in your answer? – Gelasius
Because I wish the reader to visit the site and begin the learning process. – War
U
-2

If you are working on spring/mvn project you can use this command:

mvn package -DskipTests

The jar file will be saved on target directoy.

Utilize answered 4/7, 2009 at 17:5 Comment(0)
A
-4

Ant and Maven are widely used. I prefer Ant, I feel it's more lightweight and you the developer are more in control. Some would suggest that's its downside :-)

Arianearianie answered 4/7, 2009 at 17:5 Comment(1)
This does nothing to explain why the error is happening. – Putandtake

© 2022 - 2024 β€” McMap. All rights reserved.