S01L02 – HelloWorld Java program without Maven

HelloWorld Program without Maven

Introduction

Creating a basic Java application is the first step for any developer starting their journey in Java programming. This article will guide you through the process of building a simple “Hello World” Java program without using Maven. Understanding the manual compilation and execution of Java code helps beginners to grasp the core components of the Java language, including classpath setup and manifest files.

Why Learn Without Maven?

  • Pros:
    • Better understanding of the Java compilation process.
    • Learning the structure of a standalone Java project.
    • Gaining knowledge of how to execute and package Java programs manually.

When to Use?

  • For small projects or when exploring the basics of Java.
  • When Maven is not required for dependency management.

Creating a HelloWorld Java Program

In this section, we’ll create a simple Java program that prints “Hello, World!” to the console.

1. Setting Up the Project Structure

Create a basic directory structure for your Java project as shown below:

2. Writing the HelloWorld.java File

The HelloWorld.java file contains the code to print “Hello, World!” to the console. The code is as follows:

Explanation:

  • The package statement defines the package name org.studyeasy.
  • The public class HelloWorld is the main class containing the main method.
  • System.out.println("Hello, World!"); prints the text to the console.

3. Understanding the MANIFEST.MF File

The META-INF/MANIFEST.MF file contains information about the jar file, including the entry point of the application:

  • Manifest-Version specifies the version of the manifest.
  • Main-Class indicates the class containing the main method.

Compiling and Running the Program

1. Compiling the Java Program

Navigate to the root of your project and compile the HelloWorld.java file using the following command:

  • Explanation:
    • javac is the Java compiler.
    • -d out/production/HelloWorld specifies the destination directory for the compiled classes.
    • src/org/studyeasy/HelloWorld.java is the path to the Java source file.

This command creates the HelloWorld.class file in the out/production/HelloWorld/org/studyeasy directory.

2. Running the Compiled Class File

Execute the compiled class using the following command:

  • Explanation:
    • java is the Java interpreter.
    • -cp out/production/HelloWorld sets the classpath to the directory containing the compiled class.
    • org.studyeasy.HelloWorld specifies the fully qualified name of the class to run.

The output of this command will be:

Creating and Running a JAR File

1. Packaging the Application into a JAR File

Package the compiled classes into a jar file using the following command:

  • Explanation:
    • jar is the jar tool for packaging Java applications.
    • c creates a new jar file.
    • f specifies the name of the jar file.
    • m includes the manifest file META-INF/MANIFEST.MF.
    • -C out/production/HelloWorld (changes to the directory out/production/HelloWorld and includes all files in the jar).

2. Running the JAR File

Run the created jar file using the command:

The output will again be:

Conclusion

In this article, we’ve learned how to create, compile, and run a simple Java “Hello World” application without using Maven. Understanding this process helps developers appreciate the role of tools like Maven and understand the fundamental structure of a Java project. As we progress, we’ll explore how Maven simplifies dependency management and project building.