How to create a .jar file or export JAR in IntelliJ IDEA (like Eclipse Java archive export)?

Learn how to create a .jar file or export jar in intellij idea (like eclipse java archive export)? with practical examples, diagrams, and best practices. Covers java, intellij-idea development tech...

How to Create a JAR File (Export JAR) in IntelliJ IDEA

Hero image for How to create a .jar file or export JAR in IntelliJ IDEA (like Eclipse Java archive export)?

Learn the step-by-step process to package your Java application into an executable JAR file using IntelliJ IDEA, similar to Eclipse's Java archive export.

Creating a Java Archive (JAR) file is a fundamental step in deploying Java applications. A JAR file bundles all necessary classes, resources, and metadata into a single, portable file. This guide will walk you through the process of exporting a JAR file from your IntelliJ IDEA project, covering both standard and executable JARs.

Understanding JAR Files and Artifacts in IntelliJ IDEA

Before diving into the steps, it's important to understand what a JAR file is and how IntelliJ IDEA manages build outputs. A JAR file is essentially a ZIP file containing compiled Java bytecode (.class files), associated resources (images, configuration files), and a manifest file (META-INF/MANIFEST.MF).

In IntelliJ IDEA, the concept of an 'Artifact' is used to define how your project's output should be packaged. An artifact can be a JAR file, a WAR file, an exploded directory, or other types of deployable units. For creating a JAR, you'll configure a 'JAR artifact'.

flowchart TD
    A[Start IntelliJ IDEA Project] --> B{Project Structure (Ctrl+Alt+Shift+S)}
    B --> C[Artifacts Section]
    C --> D[Add New Artifact (+)]
    D --> E["Select JAR -> From modules with dependencies..."]
    E --> F{Choose Main Class}
    F --> G[Configure Output Directory & Name]
    G --> H[Apply & OK]
    H --> I[Build Menu]
    I --> J[Build Artifacts...]
    J --> K[Select Artifact & Build]
    K --> L[JAR File Created]

Flowchart of the JAR creation process in IntelliJ IDEA

Step-by-Step Guide to Exporting a JAR File

Follow these instructions to create an executable JAR file for your Java application in IntelliJ IDEA. This process ensures all dependencies are correctly bundled.

1. Open Project Structure

Navigate to File -> Project Structure... (or press Ctrl+Alt+Shift+S on Windows/Linux, ⌘; on macOS).

2. Go to Artifacts

In the Project Structure dialog, select Artifacts from the left-hand menu.

3. Add a New JAR Artifact

Click the + (Add) button, then select JAR -> From modules with dependencies....

4. Select Main Class

In the Create JAR from Modules dialog, click the folder icon next to Main Class: to browse and select your application's main class (the one containing the public static void main(String[] args) method). IntelliJ IDEA will automatically detect available main classes. Click OK.

5. Configure JAR Settings

Review the settings. Ensure Extract to the target JAR is selected for META-INF/MANIFEST.MF creation. You can also adjust the Output directory and Name of the JAR file. Click OK to save the artifact configuration.

6. Build the Artifact

Go to Build -> Build Artifacts... from the IntelliJ IDEA menu bar. Select your newly created JAR artifact (e.g., YourProjectName:jar) and choose Build.

7. Locate the JAR File

After a successful build, the JAR file will be located in the output directory you specified (by default, it's usually in the out/artifacts/YourProjectName_jar folder within your project directory).

Running Your Executable JAR

Once your JAR file is built, you can run it from the command line using the java -jar command. This is how you would typically execute a standalone Java application.

java -jar YourProjectName.jar

Command to run an executable JAR file from the terminal.