Objectives

Upon completion of this short, you will be able to:

  • compile Java programs
  • create makefiles to compile Java programs
  • run Java programs from the shell

Install the JDK

This lessons requires that you have the Java Development Kit (JDK) installed. The JDK contains the compiler which is needed to covert Java source code to Java “binary” code – a platform independent machine language for the Java Virtual Machine (JVM). The JDK can be found at the Oracle JDK Download Page. Be sure to install the JDK for your particular CPU and Operating System.

To be able to invoke the javac Java compiler from any folder, you need to add the bin directory from the JDK installation directory to your “path” environment variable.

The installation was successful if you can successfully invoke the javac compiler from a terminal or shell prompt.

% java-version
javac 19.0.2

Compiling Java Programs

Write your program code using an editor of your choice (e.g., sublime, vim, jedit, textedit, notepad, notepad++)1 and save your code in files with a .java extension. We will compile the simple Java program below.

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

Here’s a breakdown of this program:

  • public class HelloWorld: This line declares a class named HelloWorld. In Java, every program must have at least one class, and the class name must match the file name (in this case, HelloWorld.java).

  • public static void main(String[] args): This is the main method, the entry point for Java applications. It’s where the program execution starts. It takes an array of strings as its argument, which can be used to pass command-line arguments (although we’re not using them in this simple example).

  • System.out.println("Hello, World!");: This line prints the “Hello, World!” message to the console. System.out refers to the standard output stream, and println is a method to print a line of text followed by a newline character.

To compile this program:

  1. Save the code above in a file named HelloWorld.java.

  2. Open a command prompt or terminal window.

  3. Navigate to the directory where you saved HelloWorld.java.

  4. Compile the Java source code by typing the following command:

    % javac HelloWorld.java

This will compile your Java source code into JVM bytecode.

After successful compilation, you can execute (run) the program with this command:

% java HelloWorld

Using ‘make’

If your Java program has multiple source files containing your classes, then a ‘makefile’ can simplify the compilation process.

Makefiles

“Make” is a build automation tool used primarily for compiling and building software projects. It helps automate the process of compiling source code into executable programs or libraries, managing dependencies, and ensuring that only the necessary parts of a project are recompiled when source code changes. Make is particularly popular in Unix-like operating systems, but it’s available on many platforms.

A “Makefile” is a script written in a specific format that is used by the “make” tool to define the rules and dependencies for building a project. It contains a set of instructions that specify how the project’s source code should be compiled and linked, as well as how different parts of the project relate to each other. Makefiles typically consist of targets, dependencies, and rules.

Here are the key components of a Makefile:

  1. Targets: These are the end goals or objectives of the build process. Common targets in a Makefile might include “all” (to build the entire project), “clean” (to remove generated files), and various targets for individual executables or libraries.

  2. Dependencies: Dependencies are files or other targets that the current target relies on. If a dependency has changed since the last build or doesn’t exist, the associated target will be rebuilt.

  3. Rules: Rules define how to build a target from its dependencies. They typically consist of a set of commands or shell scripts that the “make” tool executes to perform the build.

Here’s a simplified example of a Makefile for a C program:

# Define the compiler and compiler flags
CC = gcc
CFLAGS = -Wall

# Define the source and header files
SRCS = main.c functions.c
HDRS = functions.h

# Define the target executable
TARGET = my_program

# The "all" target is the default target and builds the executable
all: $(TARGET)

# The target executable depends on source files and headers
$(TARGET): $(SRCS) $(HDRS)
    $(CC) $(CFLAGS) -o $(TARGET) $(SRCS)

# The "clean" target removes generated files
clean:
    rm -f $(TARGET)

In this example, the Makefile defines the “all” and “clean” targets, specifies dependencies, and provides rules for building the target executable “my_program.” When you run the “make” command, it reads the Makefile and follows the instructions to compile and link the program. If you run “make clean,” it will remove the generated executable.

Makefiles are a powerful tool for automating the build process in software development, making it easier to manage complex projects with multiple source files and dependencies.

Building a Java ‘makefile’

Certainly! Here’s a Makefile for compiling the Java program “HelloWorld.java” mentioned earlier:

# Define the Java compiler and compiler flags
JAVAC = javac
JFLAGS = 

# Define the source file and the target class name
SOURCE = HelloWorld.java
TARGET = HelloWorld

# Default target: build the Java program
all: $(TARGET).class

# Compile the Java source file into a class file
$(TARGET).class: $(SOURCE)
    $(JAVAC) $(JFLAGS) $(SOURCE)

# Clean up generated class files
clean:
    rm -f $(TARGET).class

In this Makefile:

  • JAVAC is set to the Java compiler (javac).
  • JFLAGS is used to specify any additional compiler flags (none in this case).
  • SOURCE is the name of the Java source file (HelloWorld.java).
  • TARGET is the name of the compiled Java class (HelloWorld without the “.class” extension).

Here’s how you can use this Makefile:

  1. Save the Makefile in the same directory as your HelloWorld.java file.

  2. Open a command prompt or terminal window.

  3. Navigate to the directory where the Makefile and HelloWorld.java are located.

  4. Run the make command to compile the Java program:

    % make

    This command will use the Makefile rules to compile HelloWorld.java into HelloWorld.class.

  5. If the compilation is successful, you can run your Java program using:

    % java HelloWorld
  6. To clean up and remove the generated class file, you can use the make clean command:

    % make clean

This Makefile simplifies the process of compiling and running the Java program, and it also provides a way to clean up the generated class file when needed.

Summary

In this short, we demonstrated the process of downloading and installing the Java Development Kit (JDK) for Java programming. We provided a simple Java program that prints “Hello, World!” to the console, along with instructions on how to compile and run the program. Finally, we showed how to use Make to define build rules and dependencies. Finally, we created a Makefile to compile a Java program, demonstrating how Makefiles can automate the build process for Java applications.


All Files for Short S-9.107

Learn More

TBD

Errata

Let us know.


  1. Do not use a word processor such as Microsoft Word or TextEdit in Rich Text mode to edit code as that will include rich text formatting which will not compile. If you use MacOS’ textedit program, be sure to switch to ‘Plain Text’ mode immediately after launching by selecting “Make Plain Text” from the Format menu.↩︎