Core Java

Java Compiler Example

In this article, we’re going to see how the Java compiler works. Also, we’ll use an example to understand the compiler steps from the language to bytecode.

1. Introduction

In computation, the compilation is a process of converting the source code into machine language (bytecode). To clarify, a compiler will take our source code written in natural human language and turn it to the computer’s processor uses.

The Java compiler (javac) basically transforms our source code files (.java) into bytecodes (.class) for Java Virtual Machine (JVM) use.

In the next step, we’ll see the steps that Java uses to compile our code into machine language.

2. Pre-requisites

The minimum Java version for executing the article’s example is JDK 8 (find here), but we can use the most recently released Java version JDK 16 on Oracle’s official site or the OpenJDK version.

Also, I’m using IntelliJ 2020.2, but you can use any IDE with support for the versions recommended above.

3. Why we use a compiler?

As said before, we use a compiler to convert our source code into machine language, the bytecodes. Also, the compiler will analyze the source code syntax to make sure that the statements written by the developer are correctly referred to in the final bytecode.

The official Java compiler is called javac. It comes with the JDK installation in both versions (Oracle and OpenJDK). To check the compiler version after installing JDK into our machine, let’s use the command below:

Check javac version

$ javac -version
javac 16

Important note: while javac is the command to convert source code into bytecode, the java command in JDK is used by the JVM to effectively run our application.

4. Checking JDK version

IDE’s like IntelliJ have already an embedded way to compile and execute our application. But, for studies purposes, we’ll use the terminal to compile and run our application to understand how the compiler works in the Java environment.

First, let’s check what’s our Java version installed:

Checking JDK version

$ java -version
java version "1.8.0_181"
Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)

If you’re using the JDK 16, the same command will show the version as well.

Checking JDK 16 version

java -version
openjdk version "16" 2021-03-16
OpenJDK Runtime Environment (build 16+36-2231)
OpenJDK 64-Bit Server VM (build 16+36-2231, mixed mode, sharing)

Why did I put this information? The Java compiler command remains (almost) the same through the very first version (1.0) until the most recently (16). However, for this article, I’ll show some little difference between compilers in JDK 1.8 and JDK 16.

5. Running the compiler

Now, with our Java version defined, let’s compile our code. For this article, I create a simple code to be compiled, the famous “Hello world” script to show us how the compiler works.

Example Hello Word code

package com.examples.javacodegeeks.compiler;

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

Nothing new until here.

Moving on, in the root directory of our project, we use the command below in JDK 8 to compile the previous source code.

Compiling in JDK 8

$ mkdir out                                                                    
$ javac src/com/examples/javacodegeeks/compiler/JavaCompilerExample.java -d out

Note that I use a bash command just to create an output directory for the compiler uses to generate the bytecode file (.class). The option -d indicates where the .class file will be generated.

Now, let’s execute our compiled code using the java command:

Running application

$ cd out 
$ java com.examples.javacodegeeks.compiler.JavaCompilerExample 
Hello, world!

Not, let’s do the same using the JDK 16.

Compiling in JDK 16

$ javac src/com/examples/javacodegeeks/compiler/JavaCompilerExample.java -d out
$ cd out 
$ java com.examples.javacodegeeks.compiler.JavaCompilerExample 
Hello, world!

Note that I didn’t use the mkdir to create an output directory, because in JDK 16 the option -d already do this to us. That’s the little difference with JDK 8. The java command to execute in JVM remains the same as well.

6. Conclusion

In conclusion, we saw what’s the Java compiler and understand the difference between the source code and bytecode and why we use each one. Also, we do an example to see the differences between JDKs versions and clarify the use of javac and java commands.

8. Download the source code

Download
You can download the full source code of this example here: Java Compiler Example

Last updated on Jan. 13th, 2021

Sergio Lauriano Junior

Sergio is graduated in Software Development in the University City of São Paulo (UNICID). During his career, he get involved in a large number of projects such as telecommunications, billing, data processing, health and financial services. Currently, he works in financial area using mainly Java and IBM technologies.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Paul A. Gureghian
Paul A. Gureghian
3 years ago

Why use the full path to the source code and compiled bytecode when you are already in that folder ? just pass the source code file to javac and the compiled binary to java.

Paul A. Gureghian
Paul A. Gureghian
3 years ago

Why use the full path to the source code and compiled binary when you are already in it’s folder ? just pass the source code file to javac and the compiled bytecode to java.

Back to top button