DEV Community

Pranav Bakare
Pranav Bakare

Posted on

4 1 1 1 1

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).

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (1)

Collapse
 
luciano_03f8942652 profile image
Luciano Grossi

Excellent explanation! Thanks