📜  java makefile clean bin - Java (1)

📅  最后修改于: 2023-12-03 15:31:31.431000             🧑  作者: Mango

Java Makefile Clean Bin

Introduction

As a Java programmer, you might have come across the need to organize your code into separate directories or packages. This is where Makefile comes into play. Makefile is a build automation tool that allows you to automate the process of compiling and linking your Java code into executables.

In this tutorial, we'll focus on the 'clean' and 'bin' targets within a Makefile for Java. The 'clean' target is important when you need to delete all generated files, while the 'bin' target handles the compilation and linking of your source files.

Prerequisites

To follow this tutorial, you need:

  • Basic knowledge of the Java programming language
  • A text editor of your choice (e.g. Notepad++, Sublime Text, Visual Studio Code)
  • JDK and JRE installed on your system
  • Make utility installed on your system
Getting started

Before we dive into the details of Makefile, let's first understand the directory structure we'll be working with. Here is an example:

MyProject/
├── src/
│   ├── com/
│   │   └── myproject/
│   │       └── Main.java
│   └── Module.java
├── Makefile
└── bin/

The 'src' directory contains our Java source files, while the 'bin' directory will contain the compiled bytecode files. The 'Makefile' is where we'll define our build automation tasks.

The Makefile

Create a new file named 'Makefile' in the root directory of your project. Open it in your text editor and add the following code:

# Variables
JC = javac
JFLAGS = -d bin -cp src

# Targets
clean:
    @echo "Cleaning up..."
    rm -rf bin/* 

bin:
    @echo "Compiling..."
    $(JC) $(JFLAGS) src/**/**/*.java

.PHONY: clean, bin

Let's break down the code above. First, we define two variables, 'JC' and 'JFLAGS'. 'JC' points to the location of the 'javac' executable, while 'JFLAGS' specifies the destination directory for the compiled bytecode files, which is set to the 'bin' directory. The '-cp' flag specifies the location of the source files, which is set to the 'src' directory.

Next, we define two targets, 'clean' and 'bin'. The 'clean' target simply removes all files in the 'bin' directory. The 'bin' target compiles all Java files in the 'src' directory and outputs the compiled bytecode files to the 'bin' directory.

Finally, we define the '.PHONY' directive, which tells Makefile that 'clean' and 'bin' are not files, but rather targets.

How to use

To use the Makefile, open a terminal or command prompt and navigate to the project directory. To clean all generated files, run the following command:

make clean

This will delete all files in the 'bin' directory.

To compile and link your code, run the following command:

make bin

This will generate the compiled bytecode files in the 'bin' directory.

Conclusion

In conclusion, Makefile is a powerful tool that can help you automate your build process in Java. By defining targets for cleaning and compiling your code, you can easily manage your project and keep it organized. If you're new to Makefile, don't worry! It takes time to get the hang of it, but with practice, you'll be able to customize it to suit your needs.