Search In This Website

Kamis, 24 Juli 2025

How to Creating A JAR File Using the Command Line

Prerequisites

Before you begin, ensure you have the following tools on your system:
  • Java Development Kit (JDK) installed and configured in your system's PATH. You must download JDK from the official Java website, if you don't have JDK. JDK is required for compiling Java code (javac) and running JAR files (java).
    If you want to learn default Java Path For Windows And Linux Environment, click Default Java Path Environment >
    You must setup PATH environment, click Set The JAVA_HOME variable >
    To check the Java version, click How To Check Your Java Version >
  • A Text Editor, for example Notepad: To write your Java code and potentially to write a manifest file.
  • A Terminal or Command Prompt: To execute compilation and JAR creation commands.

Assumption

  • Ensure you have a Java class with a main method ( after You compiled a Java source code File ), which serves as the entry point for your Java simple application (JAR) or Java simple program (JAR). You can use this Java exercise, click Hello World In Java and then you must compile the Java Program, click How To Compile Java 1.
  • Ensure you understand to using Text Editor and a Terminal or Command Prompt.

The Steps

1. First we navigate to the root directory of your Java class files project directory. In this example the directory is D:\demo\java\GettingStarted\HelloWorld.

navigate to the root directory
of your Java class files project directory

list your root directory
of your Java class files project directory

2. Create a Manifest File: Open your Text Editor and then create a text file (e.g., manifest.txt) and add the Main-Class entry. Make sure there's a newline at the end of the Main-Class line. The full contents Of Manifest.txt example:
Main-Class: helloWorld
Class-Path: helloWorld.jar

Notepad program show

Type Manifest.txt example contents

3. Save file As Manifest.txt in directory D:\demo\java\GettingStarted\HelloWorld\.

click File > Save As menu

Navigate to D:\demo\java\GettingStarted\HelloWorld\.
And then type Manifest.txt in File name box.

click Save button

file Manifest.txt has saved

4. Create the JAR File
Now, use the jar command to package your compiled .class file(s) and the manifest file (if applicable) into a single .jar file, for example:
jar cvfm helloWorld.jar helloWorld helloWorld.class
The options and arguments used in this command are: 
-c: Creates a new archive.
-v: Provides verbose output, listing the files as they are added.
-f: Specifies the filename of the JAR archive (helloWorld.jar).
-m: Specifies the manifest file to use (manifest.txt).

Create the JAR File

The JAR file has created

5. Run the Executable JAR
If you created an executable JAR, you can run it directly from the Command Prompt:
java -jar helloWorld.jar
You should see the output: Hello World

Run the Executable JAR

The Executable JAR has succeed to run

Notes


<< back to  Study About Java 1

Bibliography

https://docs.oracle.com/ ( manual ebook )
https://www.cmu.edu/ ( university )
https://www.helsinki.fi/ ( university )
https://www.coursera.org/
https://www.iitk.ac.in/ ( university )
https://www.ntu.edu.sg/ ( university )

Tidak ada komentar:

Posting Komentar