Java
JAR file
command line
class execution
programming tutorial

Run class in Jar file

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

The Java programming language, with its Write Once, Run Anywhere (WORA) paradigm, makes it convenient to develop and deploy software across various platforms. A common distribution method for Java applications is through a JAR (Java Archive) file, which packages compiled Java classes and other necessary resources. One key challenge when working with JAR files is the execution of a specific class containing the application's main method. In this article, we'll explore how to specify and run the main class in a JAR file, along with related technical details and examples.

Understanding JAR Files

A JAR file is essentially a compressed archive that typically contains:

  1. Compiled Java Files: .class files generated through Java's compilation process.
  2. Resources: Any resources like text files, images, etc., needed by the application.
  3. Metadata: Usually contained within a META-INF directory, which may include a manifest file (MANIFEST.MF) that provides configuration information.

The MANIFEST.MF file can be crucial for specifying certain attributes of the JAR file, such as the main class to execute.

Specifying the Main Class

To execute a Java application bundled in a JAR file, you need to specify which class contains the public static void main(String[] args) method. This is done using the Main-Class attribute in the manifest file. Here is an example of a basic manifest file:

plaintext
Manifest-Version: 1.0
Main-Class: com.example.MyMainClass

Creating a JAR File with Manifest

The Java Development Kit (JDK) provides tools to create JAR files. You can specify the manifest incidentally or through a separate file. Below is a command-line example of building a JAR with a specified main class:

bash
# if the manifest is in a file named "manifest.txt"
jar cfm myapplication.jar manifest.txt -C build/ .

The -C option changes to a directory before adding files, effectively archiving the compiled classes and resources into the JAR.

Running the Class in a JAR File

Once you've packaged your application into a JAR file with the Main-Class appropriately defined, you can execute it using the java command:

bash
java -jar myapplication.jar

This command finds the Main-Class attribute in the manifest and runs the designated class.

Additional Features

Beyond just specifying and running the main class, Java JAR files and manifests can use attributes for other purposes, such as:

  • Class-Path: Specifies the list of JAR files or directories needed at runtime.
plaintext
  Class-Path: lib/dependency1.jar lib/dependency2.jar
  • Multi-release JARs: To offer specific class versions for different Java releases using the Multi-Release: true attribute.

Example Scenarios

Basic Application

Consider a simple Java project structure:

 
1src/
2| -- com/ | -- example/ |
3| ----------------- | ---------------------------------------------------------- |
4| **JAR File** | Java Archive that contains class files and resources. |
5| **Manifest File** | A metadata file to specify settings like the `Main-Class`. |
6| **Main-Class** | Entry point of the application
for the JAR execution. |
7| **Class-Path** | Specifies dependent libraries necessary
at runtime. |
8
9## Conclusion
10
11Running a class in a JAR file involves understanding the JAR's structure and especially the usage of the manifest file to specify the main class. By mastering these techniques, developers can effectively distribute and execute Java applications through compact and portable JAR files.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.