DEV Community

Pranav Bakare
Pranav Bakare

Posted on

3 1 1 2 2

Journey From Java file to a JAR file

Step-by-step breakdown of how we go from a Java file to a JAR file, with a detailed explanation at each step:


Step 1: Write the Java Code

Action: Create a .java file with your Java code.

Example: Suppose your Java file is named Main.java and contains:

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

Explanation: This file contains your source code written in Java, following the syntax and rules of the Java programming language.


Step 2: Compile the Java Code

Action: Use the Java compiler (javac) to compile the .java file into bytecode.

javac Main.java

Output: This generates a .class file (e.g., Main.class) in the same directory as the .java file.

Explanation: The .class file contains the bytecode, which is the machine-readable instructions executed by the Java Virtual Machine (JVM). Each .java file you compile will produce a corresponding .class file.


Step 3: Create a Manifest File (Optional)

Action: Create a MANIFEST.MF file to specify metadata for the JAR file. For example:

Main-Class: Main

Explanation: The Main-Class attribute specifies the entry point for the application, i.e., the class containing the main method. This makes it easier to run the JAR file later.


Step 4: Package the JAR File

Action: Use the jar command to package the .class file(s) and optional resources into a JAR file.

jar cvfm MyApplication.jar MANIFEST.MF Main.class

c: Create a new JAR file.

v: Enable verbose output to show the process.

f: Specify the output file name (MyApplication.jar).

m: Include the manifest file (MANIFEST.MF).

Explanation: The jar tool combines all the compiled .class files, resources (if any), and the manifest file into a single archive (MyApplication.jar). This archive is portable and can be executed or distributed.


Step 5: Test the JAR File

Action: Run the JAR file to ensure it works as expected.

java -jar MyApplication.jar

Output: If everything is set up correctly, the output will be:

Hello, World!

Explanation: The JVM reads the MANIFEST.MF file inside the JAR to find the Main-Class and executes its main method.


Step 6: Deploy the JAR File

The deployment method depends on the target environment:

a. Standalone Deployment

Copy the JAR file to the target machine.

Run it using the java -jar command.

b. Microservices Deployment

If it's a Spring Boot or similar microservice, the JAR often contains an embedded server (e.g., Tomcat). Deploy the "fat JAR" using:

java -jar MySpringBootApplication.jar

c. Containerized Deployment

Use a Dockerfile to package the application into a container.

FROM openjdk:17
COPY MyApplication.jar /app/MyApplication.jar
WORKDIR /app
CMD ["java", "-jar", "MyApplication.jar"]

Build the image:

docker build -t my-java-app .

Run the container:

docker run -p 8080:8080 my-java-app

d. Cloud Deployment

Deploy the JAR file to cloud platforms like AWS, Google Cloud, or Azure using CI/CD pipelines or specific cloud services.


Summary of Execution Flow

  1. Write Java code in a .java file.

  2. Compile the code using javac to generate .class files.

  3. (Optional) Create a MANIFEST.MF file to specify the main class.

  4. Package the .class file(s) into a JAR file using the jar tool.

  5. Test the JAR file using the java -jar command.

  6. Deploy the JAR file to the target environment (standalone, microservices, containerized, or cloud).

Billboard image

Deploy and scale your apps on AWS and GCP with a world class developer experience

Coherence makes it easy to set up and maintain cloud infrastructure. Harness the extensibility, compliance and cost efficiency of the cloud.

Learn more

Top comments (0)

Cloudinary image

Zoom pan, gen fill, restore, overlay, upscale, crop, resize...

Chain advanced transformations through a set of image and video APIs while optimizing assets by 90%.

Explore

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay