From Source to JAR – Part 1

Organizing and sharing your application in Java development can be tricky without the right tools. That’s where JAR files come in convenient. A JAR (Java ARchive) file bundles all your compiled Java code, resources, and configuration into one single file, making running and distributing your application easy.

Build tools like Gradle and Maven can help make this process smoother. They automate the creation of JAR files, handle dependencies, and manage the packaging process, saving you time and reducing errors.

In this first article, I’ll cover the basics of compiling Java classes and provide an introduction to JAR files, setting the foundation for understanding how these tools can simplify your Java projects.

1. Compiling Java Classes: The Basics

When you write Java code, it’s in plain text files with a .java extension. To run your Java program, you need to convert these files into bytecode, which the Java Virtual Machine (JVM) can execute. This process is called compiling.

Example: Simple Java Application

  • Main Class (Main.java)
public class Main {
    public static void main(String[] args) {
        Greeting greeting = new Greeting();
        System.out.println(greeting.getMessage());
    }
}
  • Helper Class (Greeting.java)
public class Greeting {
    public String getMessage() {
        return "Hello, World!";
    }
}

Compiling the Classes

To compile these classes, you use the Java compiler (javac). Open your terminal or command prompt and run:

javac Main.java Greeting.java

This command generates two .class files:

  • Main.class
  • Greeting.class

These files contain bytecode that the JVM can run.

Running the Java Application

You can now run your application with:

java Main

This command tells the JVM to execute the Main class, which uses the Greeting class to print a message to the console.

2. Introducing JAR Files

In a simple application like the one above, running your program is straightforward. However, as your application grows and starts using multiple classes and external libraries, managing all these files becomes challenging. This is where JAR files come in handy.

What is a JAR File?

A JAR (Java Archive) file is a single file that bundles together:

  • Compiled Java Classes: All your .class files.
  • Resources: Any additional files your application needs, like images or configuration files.
  • Manifest: A special file that can specify how the JAR should be run, including which class contains the main method.

Creating a JAR File

  1. Create a Manifest File (MANIFEST.MF)
Main-Class: Main

This file tells the JVM which class to run when executing the JAR file.

  1. Create the JAR File

Run the following command in your terminal:

jar cfm MyApp.jar MANIFEST.MF Main.class Greeting.class

This command creates a Myapp.jar file containing the Main.class and Greeting.class classes and the MANIFEST.MF file.

  1. Running the JAR File

Now you can run your application with:

java -jar MyApp.jar

3. Advantages of Using JAR Files

Using JAR files brings several benefits, especially as your Java projects become more complex.

Simplifies Distribution

  • Single Package: All your classes, resources, and dependencies are bundled into one file. This makes it easier to share and deploy your application.

Ease of Use

  • Executable: If the JAR is properly configured with a manifest file, you can run the application with a simple java -jar command.
  • Standardized: JAR files follow a standard format recognized by Java tools, making them compatible with most Java environments.

Better Management of Dependencies

  • Libraries: You can include external libraries within the JAR, eliminating the need to manually manage them on the user’s system.
  • Classpath Handling: Users don’t need to worry about setting the classpath correctly. Everything is included in the JAR file.

Compression

  • Size Reduction: JAR files are compressed, reducing the size of the package. This makes it quicker to transfer and download.

4. Automating Java Compilation and JAR Packaging with a Script

This section provides a script for automating the process of compiling Java files and packaging them into a JAR file. It includes steps for creating a manifest file, packaging the compiled classes and manifest into a JAR, and running the JAR file. The script also provides an option to clean up the generated files.

#!/bin/bash

#This shell script allows you to either compile Java files or create a JAR file from them.
# To use this script, you need to pass an argument to it. The argument can be either `compile` or `jar`.

# Create the Greeting.java file
echo 'public class Greeting {
    public String getMessage() {
        return "Hello, World!";
    }
}' > Greeting.java

# Create the Main.java file
echo 'public class Main {
    public static void main(String[] args) {
        Greeting greeting = new Greeting();
        System.out.println(greeting.getMessage());
    }
}' > Main.java

# Check the command-line argument
if [ "$1" = "compile" ]; then
    # Compile the Java files
    javac Main.java Greeting.java

    # Run the Main class
    java Main

    # Run the command below to remove the generated files
    # rm Main.class Greeting.class Main.java Greeting.java

elif [ "$1" = "jar" ]; then
    # Compile the Java files
    javac Main.java Greeting.java

    # Create the MANIFEST.MF file
    echo 'Main-Class: Main' > MANIFEST.MF

    # Package the compiled classes and manifest into a JAR file
    jar cfm MyApp.jar MANIFEST.MF Main.class Greeting.class

    # Run the JAR file
    java -jar MyApp.jar

    # Run the command below to remove the generated files
    #rm Main.java Greeting.java MANIFEST.MF MyApp.jar Main.class Greeting.class
else
    echo "Invalid argument. Please use 'compile' to compile the Java files or 'jar' to create a JAR file."
fi

5. Conclusion

Compiling Java classes into .class files is the first step in running a Java application. As your projects grow, using a JAR file simplifies the management, distribution, and execution of your application. By bundling everything into a single, compressed file, JARs make it easier to share and run Java programs, streamlining your development process.

Note: For more advanced projects, you can automate the creation of JAR files using build tools like Gradle or Maven, which handle dependencies and configurations more effectively.

In the next article, I’ll explain how to use Gradle and Maven to compile Java classes and create executable JAR files.

Posts in this series