Run class in Jar file
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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:
- Compiled Java Files:
.classfiles generated through Java's compilation process. - Resources: Any resources like text files, images, etc., needed by the application.
- Metadata: Usually contained within a
META-INFdirectory, 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:
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:
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:
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.
- Multi-release JARs: To offer specific class versions for different Java releases using the
Multi-Release: trueattribute.
Example Scenarios
Basic Application
Consider a simple Java project structure:
Related reading
- Run Dynamodb local as part of a Gradle Java project
- Run jar file in command prompt
- run main class of Maven project
- Run mvn spring-bootrun from parent module?
- Run Spring-boot's main using IDE
- run spring batch job from the controller
- Runnable with a parameter?
- Running a MVC app using Spring Boot Hibernate MySql

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack 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.