All about JARs
1/25/2026
Below is the starter code that we have
library-cli
|____Main.java
|____Library.java
|____Book.java
pwd:library-cli
package main;
import main.library.Library;
import main.library.Book;
public class Main{
public static void main(String[] args){
Library lib = new Library();
Book b = new Book(1,"Linux for Hackers","Mr.");
lib.addBook(b);
lib.getBooks();
}
}
package main.library;
public class Book{
int id;
String name;
String authorName;
public Book(int id,String name,String authorName){
this.id = id;
this.name = name;
this.authorName = authorName;
}
}
package main.library;
import java.lang.*;
import main.library.Book;
// unlike languages like js Java requires absolute path for packages
public class Library {
Book[] books;
int curr;
public Library(){
this.books = new Book[10];
this.curr = 0;
}
public void addBook(Book book){
this.books[curr++] = book;
}
public void removeBook(int id){
for(int i=0;i<curr;i++){
if(books[i].id==id){
this.removeIndex(i);
return;
}
}
System.out.println("book not found!");
}
public void getBooks(){
for(int i=0;i<curr;i++){
System.out.println("****************");
System.out.println(books[i].id);
System.out.println(books[i].name);
System.out.println(books[i].authorName);
}
}
private void removeIndex(int index){
for(int i=index;i<curr-1;i++){
books[i] = books[i+1];
}
curr--;
books[curr] = null;
}
}
What is Compilation?
Your .java files contain human-readable source code. The Java Virtual Machine
(JVM), which runs your program, cannot understand this directly. It needs the
code to be in a format called "bytecode." The process of converting your .java
source code into .class files containing this bytecode is called compilation.
The tool we use for this is javac, the Java compiler.
What is the Classpath?
The classpath is a crucial concept. It's a list of directories and JAR files
that the Java compiler (javac) and the JVM (java) use to find the classes they
need. When your code uses a class from another file (like Main using Library),
the compiler looks for Library.class in the locations specified by the
classpath. If it can't be found, you'll get an error. By default, the current
directory (.) is on the classpath.
1. Getting started
To keep our project clean, we will compile our .class files into a separate directory. This prevents mixing source files (.java) with compiled files (.class). Let's call this directory build.
mkdir build
2. Compiling Your Source Code
We will use the javac command to compile all of our .java files. We need to tell the compiler where to put the resulting .class files.
javac -d build Book.java Library.java Main.java
# you can run the above program by:
cd build && java main.Main
Let's break down that command:
-
javac: This is the Java compiler. -
-d build: The -d flag stands for "destination." It tells the compiler to place the compiled .class files into the build directory.javacwill automatically create the necessary packagesubdirectories(like main/ and main/library/) inside build. - Book.java Library.java Main.java: These are the source files we want to compile.
Your Java code is now compiled into bytecode, ready to be run.
3. Understanding JAR Files
Before we create the JAR file, let's understand what it is.
What is a JAR (Java ARchive) file?
Think of a JAR file as a zip file designed specifically for Java. Its main purpose is to bundle everything your application needs to run into a single, convenient file. This includes:
- Your compiled .class files.
- Any resources your application uses (like images, configuration files, etc.).
- A special file called the manifest.
The Manifest File (MANIFEST.MF)
- The manifest is a file inside the JAR that contains metadata (information about the files in the JAR).
- For our purposes, the most important piece of information we can put in the manifest is the
Main-Class.By specifying a Main-Class in the manifest, we create an executable JAR - This tells the java command where to find the main method to start the application.
- This is what allows you to run a whole application with a simple command like
java -jar MyApplication.jar. Without it, you would have to manually specify the main class on the command line, which is less convenient. - Below is what the
MANIFEST.MFfile we will create look like
Manifest-Version: 1.0
Created-By: 25.0.1 (Eclipse Adoptium)
Main-Class: main.Main
We will use the jar command, which is the tool for creating and managing JAR files.
jar cfe MyLibraryCLI.jar main.Main -C build .
# the `jar` command comes bundled as part of the JDK (Java Development Kit).
Let's break down this command in detail:
-
jar: The command to create a JAR file. -
c: A flag that means create a new archive. -
f: A flag that means we want to specify the file name of the JAR archive. The file name (MyLibraryCLI.jar) must come immediately after this set of flags. -
e: A flag that specifies the entry point, or the main class for an executable JAR. The fully qualified name of our main class (main.Main) must come immediately after the JAR file name. -
-C build: This is a very important part.The -C option tells the jar command to Change directory to the build directory first.This is so that the paths inside the JAR file are relative to the build directory (e.g.,main/Main.classinstead ofbuild/main/Main.class). If we didn't do this, the JVM wouldn't be able to find our classes. -
.: This specifies what to add to the JAR file. The.means "add all files and directories from the current directory" (which, because of the-C buildoption, is thebuilddirectory).
4. Running the java file
java -jar MyLibraryCLI.jar
Let's break it down:
-
java: This is the Java Virtual Machine (JVM), which executes Javabytecode. -
-jar: This flag tells the JVM that you want to run an application packaged as an executable JAR file. -
MyLibraryCLI.jar: This is the name of the JAR file you want the JVM to execute. The JVM will look inside this JAR for the Main-Class entry in its manifest file and start execution from the main method of that class.
- PS: You can view the contents of the
.jarusingwinRAR