Getting Started with Java

 

Elements of a Java Application

I understand that you are eager to type some code in your editor and run it to see your first Java application in action! Do not worry, your expectation will be fulfilled by the end of this tutorial. But before we move on, I would like to do through several elements that you need to know to fully understand what you are doing.

Even if you are familiar with some other programming language, know about compilation, know what an executable file is you may be interested in the following because Java works in a way that differs from C or C++.

 

Compiling and Executing Java Code

There are several steps that you need to follow to create a Java application. This tutorial shows you how to create a very simple Java application. If you need to create an enterprise application, the creation process is more complex but at its core you will find these simple steps.

The first of these steps is to write some Java code in a text editor.

Then this code has to be transformed to another format, which can be executed by your computer. This transformation is conducted by a special piece of software called a compiler. Some languages do not have a compiler; Java does. Every compiler is specific to a language.

The file produced by a compiler is often called a binary file or an executable file. Whereas you can read a source code and understand it, binary or executable files are not meant to be read by a human person. Only your computer can make sense of it.

This code contains special binary codes called byte code. This is a technical term that you may come across. The precise description of what is this byte code is beyond the scope of this tutorial.

Compiling some code may fail; your code has to be correct for the compiler to produce an executable version of it. Do not worry, this page gives you the code you are going to compile. All you need to do is copy it and paste it in your text editor.

Once the compiler produced the binary file that you need, you can execute this binary file, that will your program.

These two steps: compilation and execution require two specific pieces of software that are part of the Java Development Kit, also known as the JDK. You will see how to download the JDK for free and how to install it later in this tutorial.

Note that starting with Java SE 11 you can also merge these two steps into one, by executing a .java file directly. You can use these feature only if you are executing a program that is written in a single file. This way of executing your java application does not work if your java code spans more than one file.

 

Creating a First Java Class

The first step you need to know is that the Java code you are writing is saved in plain text files. In this tutorial, your application will be written in a single text file. Larger applications may require thousands of such files.

Java is an object-oriented language. If this technical term does not mean anything to you, do not worry, all you need to remember at this point is that all the code you write must be held in a Java class.

A Java class is created by a special declaration in a text file. Just copy the following code and paste it in your text editor. Congratulation! You have created your first Java class!

public class MyFirstClass {
}

The name of this Java class is MyFirstClass. You need to save this text in a file named MyFirstClass.java. A Java class must be saved in a file that has the same name as your class with the extension .java. This is mandatory and is in fact very convenient because you do not need to open a file to know what class is written in it.

You can give this class any name as long as it does not start with a number. There is a convention though: the name of a Java class starts with a capital letter. This is not mandatory but all Java developers follow this convention. When you become a seasoned Java developer, seeing a class that does not follow this convention will look weird to you.

If you are following this example to the letter, you should save the MyFirstClass class in a text file called MyFirstClass.java.

Just a word of warning: you should be using a plain text editor to create and save this file. Using a word processor will not work.

 

Preparing the Compilation of your First Class

Compiling is the second step you need to follow after the creation of your first class. It consists of transforming the Java code you wrote in your MyFirstClass.java file into another format that can be executed. The result of this transformation will be stored in another file created by the compiler. The name of this file will be MyFirstClass.class.

So far the only tool you have been using is a plain text editor. Compiling this class requires a compiler; something you may not have on your computer. Fortunately, you can download this compiler and use it for free. Let me guide you through this process.

As of now, downloading "Java" means downloading the Java Development Kit, also known as the JDK. The JDK contains many tools and among them are the ones you will be using to compile and run a Java application. It is officially distributed by the OpenJDK project and by Oracle.

You may have heard about some other elements, also called "Java".

The JRE stands for Java Runtime Environment. It is a subset of the JDK that is not distributed by the OpenJDK or Oracle anymore. It only contained the tools needed to run a Java application. You cannot compile your code with the tools provided in the JRE.

You may also have heard about J2EE, Java EE or Jakarta EE. All these acronyms refer to the Java Enterprise Edition. It is a set of tools and libraries to create enterprise-class applications. Java EE is different from the JDK and is outside the scope of this tutorial. You do not need Java EE to compile and run the simple application we are creating in this tutorial.

 

Setting up a Java Development Kit

You can download the JDK from different places. There is a one-stop page that always refers to the latest version of the JDK: https://jdk.java.net/. Selecting the latest "Ready for use" JDK version takes you to a page where you can download the version of the JDK you need.

From this page you can download four versions:

  • Linux/AArch64
  • Linux/x64
  • macOS/x64
  • Windows/x64

This page provides production-ready open-source builds of the Java Development Kit, an implementation of the Java SE Platform under the GNU General Public License, version 2, with the Classpath Exception.

Setting up a JDK for Windows/x64

Let us download the Windows version. What you get is a ZIP file of about 200MB that you can open with any ZIP utility software. This ZIP file contains the JDK. You can unzip the content of this file anywhere on your computer.

Once this is done you need to create an environment variable called JAVA_HOME that points to the directory where you unzipped the JDK. First you need to open a DOS prompt. If you unzipped a JDK 16 ZIP file in the D:\jdk\ directory then the command you need to type in this DOS prompt is the following:

> set JAVA_HOME=D:\jdk\jdk-16

Please note that in this example and all the others the leading > is there to show you that you need to type this command or paste it in a prompt. You should not type this character or paste it as it is not part of the set command.

You can check that the JAVA_HOME variable has been properly set by typing the following code:

> echo %JAVA_HOME%

This command should print the following:

D:\jdk\jdk-16

You then need to update your PATH environment variable to add the bin directory of your JDK directory to it. This can be done with the following command:

> set PATH=%JAVA_HOME%\bin;%PATH%

You need to be very cautious while setting up these two variables, because a single mistake like an added white space of a missing semicolon will result in failure.

Do not close this command prompt. If you close it and open it again then you will need to create these two variables again.

Setting up a JDK for Linux/x64

Let us download the Linux version. What you get is an archive file with a .tar.gz extension that you need to expand.

To expand it, you need to copy it or move it to the right directory. You can then type the following command:

$ tar xzf *.tar.gz

Please note that in this example and all the others, the leading $ is there to show you that you need to type this command or paste it in a prompt. You should not type this character or paste it, as it is not part of the tar command.

This command expands all the files with the extension .tar.gz that you have in the current directory. You can use the exact name of this file if you just need to expand it.

Executing this command may take several seconds or more, depending on your system. It creates a new directory in the current directory with the content of the JDK in it.

Once this is done you need to create an environment variable called JAVA_HOME that points to the directory where you expanded the JDK. If you expanded a JDK 16 archive file in the /home/javauser/jdk directory then the command you need to type in this shell prompt is the following:

$ export JAVA_HOME=/home/javauser/jdk/jdk-16

The exact directory depends on the distribution file you have expanded.

You can check that the JAVA_HOME variable has been properly set by typing the following code:

$ echo $JAVA_HOME

This command should print the following:

/home/javauser/jdk/jdk-16

Then you need to update your PATH variable to add the bin directory of your JDK directory to it. This can be done with the following command:

$ export PATH=$JAVA_HOME/bin:$PATH

You need to be very cautious while setting up these two variables because a single mistake like an added white space of a missing semicolon will result in failure.

Do not close this shell prompt. If you close it and open it again then you will need to create these two variables again.

You can check if everything is ok by typing the following command:

$ which java

Your shell should print the complete path to the java executable file in the bin directory of the distribution you just expanded. In this example it will print:

/home/javauser/jdk/jdk-16/bin/java

Setting up a JDK for macOS

Let us download the macOS version. What you get is an archive file with a .tar.gz extension that you need to expand.

To expand it, you need to copy it or move it to the right directory. You can then type the following command:

$ tar xzf *.tar.gz

Please note that in this example, and all the others, the leading $ is there to show you that you need to type this command or paste it in a prompt. You should not type this character or paste it as it is not part of the tar command.

This command expands all the files with the extension .tar.gz that you have in the current directory. You can use the exact name of this file if you just need to expand it.

Executing this command may take several seconds or more, depending on your system. It creates a new directory in the current directory with the content of the JDK in it. This directory has the extension .jdk.

Once this is done you need to create an environment variable called JAVA_HOME that points to the directory where you expanded the JDK. If you expanded a JDK 16 archive file in the /Users/javauser/jdk directory then the command you need to type in this shell prompt is the following:

$ export JAVA_HOME=/Users/javauser/jdk/jdk-16.jdk/Contents/Home

The exact directory depends on the distribution file you have expanded.

You can check that the JAVA_HOME variable has been properly set by typing the following code:

$ echo $JAVA_HOME

This command should print the following:

/Users/javauser/jdk/jdk-16.jdk/Contents/Home

You then need to update your PATH variable to add the bin directory of your JDK directory to it. This can be done with the following command:

$ export PATH=$JAVA_HOME/bin:$PATH

You need to be very cautious while setting up these two variables because a single mistake like an added white space of a missing semicolon will result in failure.

Do not close this shell prompt. If you close it and open it again then you will need to create these two variables again.

You can check if everything is ok by typing the following command:

$ which java

Your shell should print the complete path to the java executable file in the bin directory of the distribution you just expanded. In this example it will print:

/Users/javauser/jdk/jdk-16.jdk/Contents/Home/bin/java

 

Compiling your First Class

Once you have properly set up your JDK; the JAVA_HOME variable and the PATH variable, you are ready to compile your first class.

All the commands you will be typing now should be typed in the same prompt as the one you used to set up these two variables.

Compiling and Running Your First Java Program

Whether you followed the Windows, the Linux or the macOS path, the remaining is the same.

  1. Change to the directory where you saved your first class MyFirstClass.java. You can check that you are in the right directory by typing dir. It will show you the files you have in this directory. You should see your MyFirstClass.java file.
  2. Check that your compiler is accessible from this directory by typing the following. This command is the same whether you are on Windows or Linux.
> java -version

It should tell you which version of the javac you are currently using. If it gives you an error message then you need to check your JAVA_HOME and PATH variables as there is most probably something wrong with them.

  1. Now you are all set to compile your first code. You can type the following.
> javac MyFirstClass.java

Two things may happen at this point. You may have error messages telling you that the compiler cannot compile your code because of errors in your Java code. You will need to fix them before being able to move on.

If the compiler remains silent and does not complain about anything: congratulations! It means that your Java code has been properly compiled. Checking the content of the directory again should show a new file in it: MyFirstClass.class

 

Adding Code to Your Class to Run it

So far your class is empty; there is no executable code in it. If you were able to compile it properly then you can advance to the next step and execute some code.

Just open your MyFirstClass.java file and copy the following code in it.

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

As you may know, there is a long-standing tradition in computer science, which is to write a program that prints "Hello, World!" on the console of your application. So let us do that!

There is technical code in this class that may not be very clear to you. Do not worry; all you need to do is to compile it following the steps described in the previous section.

Make sure that the compiler created the MyFirstClass.class for you. To run it, all you need to type is the following command:

> java MyFirstClass

This should print Hello, World! on the console. If this is the case: congratulations! You have been able to run your first Java program!

 

Running the Hello World Program as a Single File Application

Starting with Java SE 11, you can run a Java application without going through the compilation step, as long as the program is written in a single file. This is the case of this simple Hello, World! application.

You can just type the following:

> java MyFirstClass.java

And it will print the Hello, World! message on the console.

 

Going Further

This first Java program showed you the basic steps every Java developer follows to run an application.

  1. Create a source in a set of .java text files
  2. Compile these files to produce a set of corresponding .class binary files
  3. Run them together as an application.

Developers that work on large applications do not use plain text editors to manage their source code; they use Integrated Development Environments. IDEs are complex software applications, specialized in software development. These applications handle the compilation of your source code automatically, they can help you to track errors in the syntax of your Java code and nail down bugs in its execution, among other things.

Some of these tools are open source and free to use.

Last update: September 14, 2021


Back to Tutorial List