Building a JAR file is the most common way to share your compiled Java application. When you pull dependencies from Maven, they’re actually contained in a bunch of JAR files. So how do you create your own JAR?
Perhaps you’re new to Java development (welcome!), or you’re moving to IntelliJ from Eclipse? Well, fortunately it’s quite easy to create a JAR file in IntelliJ IDEA with a Maven project.
Let’s see how to do it.
To create a JAR file from a Maven project in IntelliJ IDEA, go to the Maven Tool Window (View → Tool Windows → Maven), expand your project in the tree, expand Lifecycle, and then double-click on package.
Maven will compile your package, and the compiled JAR file will be written to the
Create a JAR of a Maven project
Maven is a project management tool for Java projects, used by lots of projects. Maven can download dependencies, run tests, compile your Java application and package it.
IntelliJ comes with its own Maven instance, which we can control from the IntelliJ GUI.
So whenever we want to perform Maven tasks – like compiling, running tests or packaging – we can use the tools in IntelliJ to do it.
In Maven, all of the tasks to create a JAR are defined in a phase called
package. (A phase is just a stage in a build - there are other phases like
An example Maven project
The steps below should work with any basic Maven project, but in case you want to see an example, check out this repository:
Get the code on GitHub
Here’s the POM file that I used:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.tutorialworks</groupId> <artifactId>maven-jar</artifactId> <version>1.0-SNAPSHOT</version> <properties> <!-- We specify Java 1.6 or later, otherwise the build will fail --> <maven.compiler.source>1.6</maven.compiler.source> <maven.compiler.target>1.6</maven.compiler.target> <!-- We also specify the file encoding of our source files, to avoid a warning --> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> </project>
Now let’s create a JAR file from this Maven project.
Create a JAR from your Maven project
Open your Maven project in IntelliJ IDEA (File → Open).
Make sure that the Maven tool window is visible by going to View → Tool Windows → Maven.
Expand your project in the tree, expand Lifecycle and double-click on package.
IntelliJ will run the Maven
packagephase, and you’ll see the output in another window below.
Your JAR file will be available at
Job done. 😄
What is a JAR file, anyway?
A JAR file is kinda special. As well as your compiled classes, it also contains a manifest file (
This manifest file contains some basic info about your app, but it can also contain information about other files inside the JAR.
So it’s better to use your IDE or build tool (like Maven) to help you create it.
We’ve seen how to create a JAR file for a Maven project in IntelliJ.