Packaging the Application

We can package the application into a Java ARchive file (JAR).

When the code is ready, we can package our application in a JAR. JAR files are often used to deploy an application to the production server. Once a JAR file has been built, it is called an artifact. Let's take look at how to create artifacts in IntelliJ IDEA.

Creating an Artifact

  1. Press ⌘; (macOS) / Ctrl+Alt+Shift+S (Windows/Linux) to bring up the Project Structure dialog.

  2. Select Artifacts from the left-hand menu and then press + (macOS) / Plus (Windows/Linux). Select JAR and then From modules with dependencies.

Selecting a JAR file to build

You don't need to change anything for the Module, however you do need to say which class in your project has the main method. Click the browse button to navigate to your main method. IntelliJ IDEA will show you a list of classes in your project, you only have one so select that.

Main class selected for JAR

  1. Press OK to select your class. All the other defaults are fine for this tutorial, press OK. Now you can see your new JAR file defined in the Project Structure dialog.

Hello World JAR in the Project Structure dialog

  1. If it matches the above screenshot, press OK. You have now defined how to build the JAR file, but you haven't actually built it yet. You need to build it with your build artifacts.

  2. Go to Build | Build Artifacts. You will only have one to choose from which is the one that we just defined.

Build artifacts

  1. Press Enter to build the artifact. Your status bar at the bottom-left of the screen will show you when this has completed.

Status bar

IntelliJ IDEA puts the artifact in a new folder in your out folder called HelloWorld_jar.

Project window artifact

To make sure that this JAR file was created correctly you will want to run it. We'll do this in the next step of this tutorial by using a run configuration.