Complete guide to create and run Jar files in Java

22 feb 2024 4 min di lettura
Complete guide to create and run Jar files in Java
Indice dei contenuti


Java Archive (JAR) files are essential for Java programmers, as they allow you to bundle multiple Java classes and associated resources into a single file for easy distribution and deployment. This guide provides an in-depth look at creating and running JAR files in Java, complete with hands-on examples to improve your understanding and skills.

Understanding JAR files

A JAR file is very similar to a zip file, used to aggregate many Java classes, metadata, and resources (text, images, etc.) into a single file for efficient storage and distribution. These files are platform independent, making them ideal for deploying Java applications or libraries across different environments.

Method 1: Creating with the Jar command

Step 1: Create a Java program

Before creating a JAR file, you need a Java program. For our example, we will create a simple "Hello World" application.

 public class HelloWorld {
 public static void main(String[] args) {
 System.out.println("Hello, World!");

Compile this program using the Java compiler (javac):


Step 2: Create a manifest file

A manifest file is a special file in a JAR that contains information about the files compressed in the JAR, including the entry point for an executable JAR. Here's a simple manifest file, called Manifest.txt, that points to the main class:

Main-Class: HelloWorld

Note: Make sure there is a newline at the end of the manifest file; otherwise, it may not be processed correctly.

Step 3: Creating the JAR file

With the compiled class and manifest file ready, you can now create the JAR file. Use the jar command as follows:

jar cvfm HelloWorld.jar Manifest.txt HelloWorld.class

This command creates a JAR file named HelloWorld.jar, with information about the manifest (m option), including the main class. The v option produces verbose output, f specifies the name of the jar file, and c indicates the creation of a new archive.

Step 4: Run the JAR file

To run the JAR file, use the java command with the -jar option:

java -jar HelloWorld.jar

If everything is set up correctly, you will see the output:

Hello, World!

Method 2: Creating a JAR file with Maven

Maven is a popular build automation tool used mostly for Java projects. Simplifies the process of project creation, dependency management, and JAR creation.

Step 1: Configuring pom.xml

Every Maven project has a pom.xml file at the root. This file describes the project's configuration, dependencies, and build settings. To create a JAR file, make sure your pom.xml includes the following basic structure:

<project xmlns=""

 <!-- Your dependencies here -->

Step 2: Create the JAR file

With pom.xml configured, go to your project directory in terminal or command prompt and run:

mvn package

Maven compiles your project and packages it into a JAR file, typically found in your project's target/directory. The command also runs any tests by default, ensuring the package is as error-free as possible before packaging.

Method 3: Creating a JAR file with Spring Boot

Spring Boot is an extension of the Spring framework that makes it easier to set up and develop new Spring applications. It favors convention over configuration and is designed to get you up and running as quickly as possible.

Step 1: Setting up your Spring Boot project

Spring Boot projects can be initialized using the Spring Initializr website ( ) or via the IDE with Spring Boot support. The generated project includes a pom.xml (for Maven) or build.gradle (for Gradle) file, preconfigured for Spring Boot applications.

For Maven, the pom.xml file will include the Spring Boot startup parent, which simplifies Maven configurations:


Step 2: Create the application

Create your application class with the @SpringBootApplication annotation. For example:

package com.example.helloworld;

 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;

 public class HelloworldApplication {

 public static void main(String[] args) {, args);


Step 3: Creating the JAR file

With Spring Boot, creating a JAR file is simple. If you are using Maven, run:

mvn clean package

This command cleans up the target/ directory, compiles the project, runs tests, and packages the compiled code into a JAR file inside the target/directory. The Spring Boot Maven plugin automatically includes all the necessary dependencies and configures the manifest for you.

The resulting JAR is an "executable JAR", which includes an embedded web server (like Tomcat), which makes it easy to run your Spring Boot application with a simple command:

java -jar target/hello-world-1.0.jar

Best practices for working with JAR files

  • Organize your files: Keep source files, compiled classes, and manifest files organized in separate directories to simplify the JAR creation process.
  • Use descriptive names: Choose clear, descriptive names for your JAR files to make it easier to identify their purpose.
  • Versioning your JARs - Include version information in your JAR file names or manifest files to manage dependencies effectively.
  • Protect your JAR files: If you distribute JARs publicly, consider signing them with a digital certificate to ensure their integrity and authenticity.


JAR files are a powerful feature of Java, making it easier to distribute and deploy applications and libraries. By following the steps outlined in this guide, you'll be well on your way to mastering creating and using JAR files in your Java projects. While manually creating JARs provides fundamental understanding, tools like Maven and frameworks like Spring Boot significantly simplify the development and deployment process. By automating tasks like dependency management and packaging, these tools allow developers to focus more on writing code and less on configuration, making Java application development more efficient and error-free.

Buy me a coffeeBuy me a coffee

Supportaci se ti piacciono i nostri contenuti. Grazie.

Successivamente, completa il checkout per l'accesso completo a
Bentornato! Accesso eseguito correttamente.
Ti sei abbonato con successo a
Successo! Il tuo account è completamente attivato, ora hai accesso a tutti i contenuti.
Operazione riuscita. Le tue informazioni di fatturazione sono state aggiornate.
La tua fatturazione non è stata aggiornata.