Java Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

> CHECK OUT THE COURSE

Authors Top

If you have a few years of experience in the Java ecosystem, and you’d like to share that with the community, have a look at our Contribution Guidelines.

1. Overview

In this tutorial, we're going to learn how we can interact with the Java compiler through the command-line interface.

As a prerequisite, we need to have Java downloaded and the JAVA_HOME  environment variable configured in our machine.

2. Compiling a Single Java Source Code File

Java provides a simple tool – javac to compile java source code files. Let's start with compiling a small class, Car.java:

public class Car {
    private String make;
    private String model;

   // standard setters and getters
}

We can compile this from a single command within the directory where this file is located:

javac Car.java

If everything works without error, there will be no output. The compiler will create the Car.class, which contains the bytecode, in the current working directory.

3. Compiling Multiple Source Code Files

Usually, our programs use more than a single class file. Let's now see how we can compile a simple program with multiple classes.

First, let's add two new types, Owner.java and History.java:

public class Car {
    private String make;
    private String model;
    private Owner owner;
    private History history;
}
public class Owner {
    private String name;
}
public class History {
    private String details;
}

Now, we need to run the below command to compile:

javac Owner.java Car.java History.java

We should note that since the classes used by the Car class are in the same directory, it's actually optional whether we specify them. We could still just compile Car.java.

4. Essential Java Compiler Options

So far, we have just used the javac command without any extra options by just passing our class names as parameters. However, we can also customize it. We can tell the java compiler where to find classes of our libraries, the base path where our code resides, and where to generate the eventual result.

Let's take a closer look at some of these options.

  • -cp or -classpath
  • -sourcepath
  • -d (directory)

4.1. What Is the -cp or -classpath Option?

Using the classpath, we can define a set of directories or files such as *.jar, *.zip that our source code depends on during compilation. Alternatively, we can set the CLASSPATH environment variable.

We should note that the classpath option has higher precedence than the environment variable.

If none of them are specified, then the classpath is assumed to be the current directory. When we wish to specify multiple directories, the path separator is ‘:‘ for most operating systems except Windows, where it's ‘;‘.

4.2. What Is the -sourcepath Option?

This option makes it possible to specify the top directory where all of our source code that needs compilation resides.

If not specified, the classpath gets scanned for the sources.

4.3. What Is the -d Option?

We use this option when we want to have all compiled results in one place, separate from the source code. We need to keep in mind that the path we want to specify must exist beforehand.

During compilation, this path is used as a root directory, and sub-folders are created automatically according to the package structure of the classes. If this option is not specified, every single *.class file is written next to their corresponding source code *.java file.

5. Compiling With an External Library

Besides the classes we create, we also need to use external libraries in our programs. Let's now take a look at a more complex example:

libs/
├─ guava-31.1-jre.jar
model/
├─ Car.java
├─ History.java
├─ Owner.java
service/
├─ CarService.java
target/

Here, we've organized our classes into packages. Additionally, we've introduced the target and the libs directories to place compiled results and libraries, respectively.

Let's say we want to use the ImmutableSet class provided by the Guava library. We download and place it under the libs folder. Then, under the service package, we introduce a new class that uses the external library in CarService.java:

package service;

import model.Car;
import java.util.Set;

import com.google.common.collect.ImmutableSet;

public class CarService {

    public Set<Car> getCars() {

        Car car1 = new Car();
        Car car2 = new Car();

        ImmutableSet<Car> cars = ImmutableSet.<Car>builder()
          .add(car1)
          .add(car2)
          .build();
        return cars;
    }
}

Now, it's time to compile our project:

javac -classpath libs/*:. -d target -sourcepath . service/CarService.java model/*.java

We've included the libs folder in our classpath with -cp.

libs/
├─ guava-31.1-jre.jar
model/
├─ Car.java
├─ History.java
├─ Owner.java
service/
├─ CarService.java
target/
├─ model/
│ ├─ Car.class
│ ├─ History.class
│ ├─ Owner.class
├─ service/
│ ├─ CarService.class

As we can see, javac successfully resolved the external ImmutbleSet class and placed the compiled classes in the target folder.

6. Conclusion

In this article, we learned how we could compile multiple source code files even when we have dependencies on external libraries.

Additionally, we took a quick look at some essential options that we can take advantage of during complex compilation tasks.

Java bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
Generic footer banner
Comments are closed on this article!