TP 1: First "Hello World!" program

Overview

During this lab session, we will introduce the Integrated Development Environment (IDE) named Eclipse. We will learn to create and execute a simple Java program of the type "Hello World" using Eclipse. Then, we will explore how to achieve the same result using the terminal to understand how to compile and execute a Java program without relying on an IDE.

In this lab, you will learn to:


First contact with Eclipse

Launch Eclipse from the menus of your Linux or Windows environment.

Explorer and project

  1. In the main Eclipse window that appears, locate a panel named Package Explorer as shown below:

    figure01.png

  2. Inside the Package Explorer, click on Create a Java project.

  3. A new window appears (see below) asking you to enter the project name.

    figure0201.png

  4. In the Project name field, enter tp01.

  5. At the bottom of the window, locate the Create module-info.java file option (see Note 1). Ensure that this option is unchecked. If it is checked, uncheck it.

  6. Leave all other options in the window unchanged.

  7. Click the Finish button.

Note 1: The concept of modules, which we will not use for this course, is designed to improve the modularity of Java applications, particularly those of very large size.

  1. The created project (tp01) appears in the Package Explorer panel (see below).

    figure0202.png

  2. Click on the small triangle (>) to the left of the project name tp01. The triangle turns downward () and the project contents are revealed (see below).

    figure03.png

  3. The src folder (abbreviation for source) appears, intended to contain the Java source code of your programs, along with the JRE System Library folder, which contains all the libraries that Java makes available to you. By clicking on the triangle (>) to the left of the JRE System Library folder, you can see the list of these libraries (see below).

  4. These libraries are sets of predefined classes made available by Java. We will study some of them during the course. By clicking again on the triangle () to the left of the JRE System Library folder, you can close this folder.

    figure0302.png

Creating a class

We are now going to create a class inside a package.

  1. First, right-click on the src folder. In the menu that appears, hover the mouse over New. A submenu will appear; click on Package. In the window that appears, enter tp01 as the package name and click Finish.

  2. Now, right-click on the newly created tp01 package. In the menu that appears, hover the mouse over New. A submenu will appear, hover the mouse over Class and click (see below).

    figure0401.png

  3. A new window like the one in the screenshot below appears.

    figure0402.png

  4. In the Name field, write HelloWorld, then select the checkbox shown in front of public static void main(String[] args).

  5. Click on the Finish button.

In the Package Explorer view, the class file named HelloWorld.java appears. In the central panel to the right of the Package Explorer, the Java code for this class is displayed. This panel also serves to edit the class code.

figure0501.png

In this class, everything between the character strings /** and */ and everything between the character string // and the end of the line are areas dedicated to writing comments. These areas were generated by Eclipse. Also note the public keywords before the class and main method declarations. We will see the meaning of these keywords in a later lesson.

Remember: A method such as the main method is the one that will be executed when the program is launched.

  1. This method, generated by Eclipse, is empty. In its body, write the following instruction:

    System.out.println("Hello World!");
    
  2. You should get:

    figure0502.png

  3. Save the file.

By default, Eclipse will compile the class as soon as its file is saved. The function of this instruction is to write the string "Hello World!" to what is called the Console.

  1. To execute this program, click on the round green button with a white triangle inside located in the menu bar (shown below).

    figure0601.png

Eclipse will then execute the class, which will result in running the main method of your class, which serves as the entry point of the program. The Console panel will appear at the bottom of the code window. There, you will see the result of the print instruction. The program execution is immediately completed (shown below).

figure0602.png

You have just executed your first program!

Compiling and executing a Java program via command line (without IDE)

As you have seen, creating a simple class and running its program is very easy with an IDE, which allows for more productive application development. But how would we do it without an IDE?

Java code is first compiled into binary code. The execution of a Java program is then done by interpreting this binary code using a JVM (Java Virtual Machine). The installation of this machine is carried out by deploying a JRE (Java Runtime Environment), which provides the virtual machine to run Java programs, or a JDK (Java Development Kit), which additionally includes the compiler (javac) and other utilities such as javadoc for automatic generation of documentation in the form of HTML pages.

These various executables are used via the terminal, a tool you have likely used before.

Compile your Hello World! program via the command line

As mentioned above, to compile and execute a Java program, you need a JDK environment specific to the given operating system (Windows, Linux, Mac). The virtual machine provided by this environment will interpret the compiled binary code to translate it into system-specific instructions. There are various providers of JDK/JRE environments, the most well-known being Oracle and the OpenJDK project.

The executables used to compile and execute Java programs are stored in a subdirectory named Java/<version_name>/bin, which is created in the operating system's program directory during the installation of the virtual machine.

  1. To find out which version of Java is installed, type the following command in a terminal:

    java -version
    
  2. This version corresponds to the most recent version of the Java language supported by the virtual machine. For this exercise, please ensure that the Java version is higher than 1.8.

For a given version of Java, there are two types of virtual machine installations: the JRE installation mentioned earlier and the JDK (Java Development Kit) installation. The JDK installation includes everything contained in a JRE, as well as the Java source code and the javac executable used for compilation.

Installing the virtual machine

If you are working on your own computer (and not a school computer) and receive a message stating that the javac command is not found during the following exercise, install a JDK on your computer. You can use the JDK provided by Oracle or the one from the OpenJDK project.

Note 2: Recent versions of Eclipse include their own JDK, which can also be used. If applicable, it will be located in the Eclipse installation directory under:

../eclipse/plugins/org.eclipse.justj.openjdk.hotspot.jre.full.<version>

However, this may depend on how Eclipse was installed (for example, via an installer or an archive) or even on the operating system.

  1. To find out the directory of the JVM used by Eclipse, simply click on the menu Window > Preferences. In the dialog box that appears, select the branch Java/Installed JREs, as illustrated in the screenshot below. The right side of the window will indicate the installation directory of the JVM used by Eclipse.

    figure0801.png

  2. If you have not already done so, in a terminal, navigate to the src directory of your "Hello World!" program's Eclipse project.

  3. To find this directory, in Eclipse, select the src folder in the package explorer, then right-click and select the Properties menu. In the window that appears, select the Resource branch. The src folder's directory is displayed on the right side of the window as indicated by the Location label (Shown bellow). A button also allows you to open a file browser directly positioned in the directory.

    figure0802.png

  4. Then type the command

    javac tp01/HelloWorld.java
    

Examine the contents of the src/tp01 directory containing the Java file. You will see that running the javac program has produced another file named HelloWorld.class from the HelloWorld.java file. This file contains the binary code generated by compiling the Java code.

It is possible to specify a directory (different from src) to javac for storing compiled files. For example, during compilation, Eclipse will store the compiled files in a different directory named bin (for binary) located at the same level as src in the project directory.

Execute your "Hello World!" program

The HelloWorld.class file containing the compiled code can now be executed by the virtual machine.

  1. To do this, run the java executable with the class name as a parameter. While still being in the src directory, type the command:

    java tp01/HelloWorld
    
  2. You should see the following string displayed in the terminal.

    Hello World!
    
  3. It is also possible to perform these two operations ( that is, Nr. 6 and 7) in a single command with the Java executable. First, delete the compiled HelloWorld.class file and verify that it has been deleted. Then, type the command (see also Note 3):

    java tp01/HelloWorld.java
    
  4. You will see the string Hello World! displayed in the terminal.

    Hello World!
    

Note 3: Executing the program in this way, note that the compiled file was not saved to disk. It was simply created and then executed without being saved. Thus, it is preferable to first compile a program using javac (which is what Eclipse does), as the program can then be executed as many times as desired without recompiling.

Note 4: These commands work for a class contained in a package1 named tp01 (that is, its .java file is directly in the tp01 subdirectory of src). If you have declared your class in another package (for example, a package named test), its .java file will be contained in a subdirectory named test of src. To execute this class, you must position yourself in the src directory and prefix the class name with its package name test instead of tp01.

If you try to execute the class in the test subdirectory, you will see an error message such as

Error: Could not find or load main class HelloWorld

You must therefore verify that you are launching the commands from the src directory.

These simple commands show us how Eclipse (or any other IDE) compiles and executes a Java program for us. In Eclipse, compilation is executed each time a Java file is saved. Thus, when a project contains multiple Java files, Eclipse will only recompile the modified files and those that depend on them, which is called incremental compilation, minimizing the compilation time.

1

We will cover the concept of packages in a later course.


Committing your project to GitLab (Optional)

Note: This step is optional for this lab but highly recommended, as you will need to use Git throughout the course and for your final project.

Now that you have created your first Java project, it is important to learn how to version your code using Git and store it in a remote repository on GitLab. This will allow you to track changes, collaborate with others, and back up your work.

Creating a GitLab repository

  1. Go to GitLab and log in with your school credentials.
  2. Click on New project (or the + button in the top menu).

figure0901.png

  1. Select Create blank project.

figure0902.png

  1. In the Project name field, enter your project name following the format: tp01-oojava.
  2. Set the visibility level to Private.
  3. Uncheck Initialize repository with a README (we will initialize it locally).
  4. Click Create project.

figure0903.png

  1. GitLab will display a page with instructions. Keep this page open as you will need the repository URL.

figure0904.png

Initializing Git in your project

  1. Open a terminal and navigate to your Eclipse project directory (the parent of the src folder).

  2. Initialize a Git repository:

    git init
    
  3. Configure your Git identity (if not already done):

    git config user.name "Your Name"
    git config user.email "your.email@telecom-paris.fr"
    
  4. Create a .gitignore file to exclude compiled files:

    echo "bin/" > .gitignore
    echo "*.class" >> .gitignore
    

Committing and pushing your code

  1. Add all files to the staging area:

    git add .
    
  2. Create your first commit:

    git commit -m "Initial commit: Hello World program"
    
  3. Add the remote GitLab repository (replace <your-repository-url> with the URL from step 8):

    git remote add origin <your-repository-url>
    
  4. Push your code to GitLab:

    git push -u origin main
    

    Note: If your default branch is named master instead of main, use:

    git push -u origin master
    
  5. Refresh your GitLab project page in the browser. You should now see your project files in the repository.

You have successfully versioned and pushed your first Java project to GitLab!