Installing development tools

Install the necessary tools

One of Java’s flagship principles is its virtual machine, which ensures all Java developers that a program can be used with all operating systems on which a Java virtual machine is installed.
During the compilation phase of our source code, this one takes an intermediate form called byte code  : it is the famous code unintelligible for your machine, but interpretable by the Java virtual machine. The latter has a name: it is more commonly known as JRE ( J ava R untime E nvironment). No need to worry about the specificities of a particular OS ( operating system).

In order to simplify our life, we will use a development tool, IDE ( I ntegrated D evelopment E nvironment), to help us write our future source codes … So we will need different things in order to be able to create programs Java: the first is this famous JRE!

Install the necessary tools

JRE or JDK

Begin by downloading the Java environment to the Oracle site , as shown in the following figure. Choose the latest stable version.

Download Insert
Download Insert

You may have noticed that you are downloading either the JRE or the JDK ( J ava D evelopment K it). The difference between these two environments is written, but for people angry with English, be aware that the JRE contains everything necessary for your Java programs to run on your computer; The JDK, in addition to containing the JRE, contains everything necessary to develop, compile …

Since IDE already contains everything needed for development and compilation, we need only the JRE. Once you click on Download JRE, you will arrive on the page shown in the following figure.

Choice of Operating System
Choice of Operating System

Check the box: Accept License Agreementand click on the link corresponding to your operating system ( x86for a 32-bit system and x64for a 64-bit system). A popup must then appear.

I told you that Java allows to develop different types of applications; So there are environments to create programs for different platforms:

  • J2SE( Java 2 Standard Edition , the one that interests us in this book): allows to develop applications called “heavy client”, for example Word, Excel, the OpenOffice.org suite … All these applications are “heavy clients”. This is what we will do in this course

  • J2EE( Java 2 Enterprise Edition ): allows to develop Web applications in Java. We also talk about thin clients.

  • J2ME( Java 2 Micro Edition ): allows to develop applications for portable devices, such as mobile phones, PDAs …

Eclipse IDE

First of all, a few words about the Eclipse project. “Eclipse IDE” is a free development environment for creating programs in many programming languages ​​(Java, C ++, PHP …). This is the tool we will use to program.

I invite you to download Eclipse IDE . Once the download page is chosen Eclipse IDE for Java Developers, choose the version of Eclipse corresponding to your OS ( Operating System ) as shown in the following figure.

Windows Version of Eclipse IDE
Windows Version of Eclipse IDE

Now select the mirror you want to use to get Eclipse. So, you just have to wait for the download to complete.

For those who had guessed it, Eclipse is the small software that will allow us to develop our applications or our applets, and also the one that will compile all that. Our software will allow us to translate our future Java programs into a byte code , understandable only by your newly installed JRE.

The specificity of Eclipse IDE comes from the fact that its architecture is totally developed around the notion of plugin . This means that all of its features are developed as plugins. To make it short, if you want to add functionality to Eclipse, you need to:

  • Download the corresponding plugin;

  • Copy the specified files to the specified directories;

  • Start Eclipse, and that’s it!

You should now have an archive containing Eclipse. Unzip it where you want, enter this folder and launch Eclipse. At startup, as shown in the following figure, Eclipse asks you in which folder you want to save your projects; Know that nothing prevents you from specifying a folder other than that proposed by default. Once you have completed this step, you will be taken to the Eclipse home page. If you want to take a look, go for it!

You must indicate where to save your projects
You must indicate where to save your projects

Quick overview of the interface

I will now make you take a quick tour of the Eclipse interface. Here are the main menus:

  • File: This is where we can create new Java projects, save them and export them if necessary.
    The shortcuts to remember are:

    • ALT+ SHIFT+ N: New project;

    • CTRL+ S: Save the file where you are positioned;

    • CTRL+ SHIFT+ S: Save everything;

    • CTRL+ W: Close the file where you are positioned;

    • CTRL+ SHIFT+ W: Close all open files.

  • Edit: In this menu, we can use the commands ” copier“, ” coller“, and so on.

  • Window: In this one, we can configure Eclipse according to our needs.

The toolbar

The toolbar looks like the following figure.

The Eclipse Toolbar
The Eclipse Toolbar

We have in order:

  1. nouveau général: Clicking this button is equivalent to doing Fichier > Nouveau;

  2. enregistrer: Amounts to doing CTRL+ S;

  3. Print: do I need to specify?

  4. exécuter la classe ou le projet spécifié: We shall see this in more detail;

  5. créer un nouveau projet: Amounts to doing Fichier > Nouveau > Java Project;

  6. créer une nouvelle classe: Create a new file. This amounts to doing Fichier > Nouveau > Classe.

Now, I will ask you to create a new Java project, as shown in the following figures.

Creating Java Project - Step 1
Creating Java Project – Step 1
Creating Java Project - Step 2
Creating Java Project – Step 2

Fill in the name of your project as I did in the first box of the second figure. You can also see where this project will be saved. A little more complicated now: you have a Java environment on your machine, but in case you have more than one, you can also specify to Eclipse which JRE to use for this project, as on the second box of the second figure. You can change this at any time in Eclipse by going into Window > Preferences, unfolding the tree Javain the window and choosing Installed JRE.

You should have a new project in the left window, as shown in the following figure.

Project Explorer
Project Explorer

To loop the loop, let’s add a new class right now in this project as we learned to do it earlier via the toolbar. The following figure shows the window on which you should fall.

Creating a class
Creating a class

In Box 1, we can see where our Java files will be stored. In Box 2, name your Java class; I chose “sdz1”. In Box 3, Eclipse asks if this class has something special. Well yes ! Check
public static void main(String[] args)(we’ll come back to this later) and click Finish. The main Eclipse window launches, as shown in the following figure.

Eclipse main window
Eclipse main window

Before we start coding, let’s explore the workspace. In the left-hand box (green) you will find the folder of your project and its contents. Here, you can manage your project as you see fit (adding, deleting …). In the box in the center (blue), I think you guessed: this is where we write our source codes. In the lower box (the red), you will see the content of your programs … and any errors! And finally, in the right-hand box (the purple), as soon as we have learned to code our own functions and objects, the list of methods and variables will be displayed.

Your first program

As I have repeatedly told you, Java programs are precompiled byte code (by your IDE or by hand) before being used by the virtual machine. This byte code is understandable only by a JVM, and this is the one that will link this code to your machine.

You probably noticed that on the JRE download page several links were available:

  • A link for Windows;

  • A link for Mac;

  • A link for Linux.

This is because the Java virtual machine presents itself differently depending on whether you are on Mac, Linux or Windows. On the other hand, the byte code remains the same regardless of the environment with which your Java program has been developed and precompiled. Direct consequence: Whatever the OS under which a Java program was coded, any machine can run it if it has a JVM!

You keep on repeating us byte code here and there, byte code there … But what exactly is that?

Well, a byte code (there are several types of byte code, but here we are talking about the one created by Java) is nothing but an intermediate code between your Java code and the machine code. This particular code is in the precompiled files of your programs; In Java, a source file has an extension .javaand a precompiled file has the extension .class: it is in this file that you will find byte code. I invite you to examine a file .classat the end of this part (you will have at least one), but I warn you, it is illegible!

On the other hand, your files .javaare simple text files whose extension has been changed. So you can open, create, or update them with Windows Notepad, for example. This means that you can write Java programs with Notepad or Notepad ++ if you wish.

Let. You should know that all Java programs are composed of at least one class . It must contain a method called main: this will be the starting point of our program.

A method is a sequence of instructions to execute. It’s a logical piece of our program. One method contains:

  • A header: this one will be somehow the identity card of the method;

  • A body: the content of the method, delimited by braces;

  • A return value: the result that the method will return.

I asked you to create a Java project; Open it! You see the famous class I was talking about? Here, it is called “sdz1”, as in the following figure. You can see that the word classis preceded by the word public, whose meaning we will see when we program objects.

Main method
Main method

For now, what you need to remember is that your class is defined by a keyword ( class), that it has a name (here “sdz1”) and that its contents are delimited by braces {{} ). We will write our source codes between braces of the method main. The syntax of this method is always the same:

public static void main(String[] args){
//Contenu de votre classe
}

Excuse us, but … why did you write “// Content of your class” and not “Contents of your class”?

Good question ! I told you before that your Java program, before it can be executed, must be precompiled in byte code. Well, the ability to force the compiler to ignore some instructions exists! This is called comment , and two syntaxes are available to comment on its text:

  1. The uniline comments: introduced by the symbols “//”, they put everything that follows them in comment, as long as the text is on the same line;

  2. The multiline comments: they are entered by the symbols “/ *” and end with the symbols “* /”.

public static void main(String[] args){
//Un commentaire
//Un autre
//Encore un autre
/*
Un commentaire
Un autre
Encore un autre
*/
Ceci nest pas un commentaire !
}

Okay, but what’s the use?

It’s simple: in the beginning, you will only make very small programs. But as soon as you have taken the bottle, their sizes and the number of classes that will compose them will increase. You will be happy to find a few lines of comments at the beginning of your class to tell you what it serves, or comments in a method that does complicated things to know where you are in your treatments …

There is actually a third syntax, but it has a particular utility. It will generate a documentation for your program (it is called “Javadoc” for “Java Documentation”). I will speak very little, and not in this chapter. We will see this when we program objects, but for the curious, I advise you the very good course of dworkin on this subject available on OpenClassrooms.

From now on and until we’re programming graphical interfaces, we’re going to do what we call procedural programs. This means that the program will be executed procedurally, that is, it is done from top to bottom, one line after the other. Of course, there are instructions that allow you to repeat pieces of code, but the program itself will end once you reach the end of the code. This is in contrast to event-driven (or graphical) programming, which is based on events (mouse click, choice in a menu …).

Bonjour Monde

Now, try to type the following code:

public static void main(String[] args){
System.out.print(“Hello World !”);
}

Once you have entered this line of code into your method main, you must run the program. If you remember the previous presentation, you have to click on the white arrow in a green circle, as in the following figure.

Program start button
Program start button

If you look in your console, in the bottom window under Eclipse, you should see something like the following figure.

The Eclipse Console
The Eclipse Console

Let’s explain this line of code.

Literally, it means “the method print()will write” Hello World! “Using the object outof the class System“. Before you pluck your hair, here are some details:

  • System: This corresponds to the call of a class called “System”. It is a utility class that mainly allows to use the standard input and output, that is to say the keyboard input and the on-screen display.

  • out: Object of the class Systemthat handles the standard output.

  • print: Method that writes to the console the text passed as a parameter (between brackets).

Consider the following code:

System.out.print(“Hello World !”);
System.out.print(“My name is”);
System.out.print(“Cysboy”);

When you run it, you should see strings that follow each other without a line break. In other words, this will be displayed in your console:

Hello World! My name isCysboy

I doubt that you would like to insert a line wrap so that your text is more readable … To do this, you have several solutions:

  • Either you use an escape character here \n ;

  • Either you use the method println()instead of the method print().

So if we go back to our previous code and apply it, here’s what it would mean:

System.out.print(“Hello World ! \n”);
System.out.println(“My name is”);
System.out.println(“\nCysboy”);

With the result:

Bonjour Monde !
My name is
 
Cysboy

You can see that:

  • When you use the escape character \n, regardless of which method is called, it immediately adds a newline to its location;

  • When you use the method println(), it automatically adds a newline at the end of the string passed as a parameter;

  • An escape character can be set in the method println().

I take this opportunity to mention two other escape characters:

  1. \rWill insert a carriage return, sometimes also used for line breaks;

  2. \tWill make a tab.

I now suggest you spend some time on compiling your command line programs. This part is not obligatory, far from it, but it can only be enriching.

Command line compilation (Windows) (optional)

Welcome to the most curious! Before you learn how to compile and run a command line program, you will have to JDK ( J ava SE D evelopment K it). That’s where we’ll have compilation of our programs. The necessary to run the programs is in the JRE … but it is also included in the JDK. I invite you to return to the Oracle site and download the Oracle site. Once you have done this, you are advised to update your environment variable %PATH%.

Uh … what?

Your “environment variable”. Because of this, Windows finds executables without having to specify the full path. You – finally, Windows – have several, but we will be interested in only one. Basically, this variable contains the path to some programs.

For example, if you specify the path to an X program in your environment variable and you have lost it in the meanders of your PC. Well you can run it by doing Démarrer > Exécuterand typing the command X.exe(assuming that the name of the executable is “X.exe”. ;)

Okay, but how do we do it? And why should we do this for the JDK?

I arrive there. Once your JDK is installed, open the JDK directory bin, as well as the JRE directory. We will dwell on two files.

In the directory binof your JRE, you must have a named file java.exe, which you also find in the directory binof your JDK. It is thanks to this file that your computer can launch your programs through the JVM. The second is only in the directory binof your JDK, it is javac.exe(Java compiler). This is the one that will precompile your Java programs by byte code.

So, why update the environment variable for the JDK? Well, compiling and running on the command line is like using these two files and specifying where the files to be processed are. This means that if we do not update the Windows environment variable, we would need to:

  • Open the command prompt;

  • Position itself in the binJDK directory;

  • Call the desired command;

  • Specify the path of the file .java ;

  • Fill in the file name.

With our updated environment variable, we will only have to:

  • Position ourselves in the file of our program;

  • Call the command;

  • Fill in the name of the Java file.

Go to the control panel of your PC; From there, click on the icon Système ; Choose the tab Avancéand you should see at the bottom a button named Variables d'environnement : click on it. A new window opens. In the lower part titled Variables système, look for the variable Path. Once selected, click Modifier. Again, a window, smaller this one, opens in front of you. It contains the name of the variable and its value.

To do this, go to the end of the value of the variable, add a semicolon if there is none and add the path to the directory binof your JDK, ending this one By a semicolon! At home, it gives this: C:\Sun\SDK\jdk\bin.

Previously, my environment variable contained, before my addition:

%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;

And now :

%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\Sun\SDK\jdk\bin;

Validate the changes: you are now ready to compile in command line.

To do this, go to the directory of your first program and delete it .class. Then press Démarrer > Exécuter(or Windows+ key Rand type “cmd”.

For example, when I open the console, I find myself in the folder C:\toto\titiand my application is in the folder C:\sdz, so I do:

Cd ..
Cd ..
Cd sdz

After the first instruction, I find myself in the file C:\toto. With the second instruction, I get to the root of my disk. Via the third instruction, I find myself in the file C:\sdz. We are now in the folder containing our Java file! That said, we could condense this by:

Cd ../../sdz

Now, you can create your file .classby running the following command:

Javac <filename.java>

If you have a file in your folder test.java, compile it by doing:

Javac test.java

And if you do not have any error messages, you can verify that the file test.classis present by using the statement dirthat lists the contents of a directory. Once this step is complete, you can start your Java program by doing the following:

Java <filenameClassInExtension>

Which gives us :

Java test

And normally, the result of your Java program appears beneath your eyes!

You have compiled and run a Java program on the command line … You could see that there is nothing really complicated and, who knows, you may need it one day.

In summary

  • The JVM is the heart of Java.

  • It runs your Java programs, precompiled byte code.

  • The files containing the source code of your Java programs have the extension .java.

  • Precompiled files that match your Java source code have the extension .class.

  • The byte code is an intermediate code between your program and the one your machine can understand.

  • A Java program, coded under Windows, can be precompiled under Mac and finally run under Linux.

  • Your machine CAN NOT understand the byte code, it needs the JVM.

  • All Java programs are composed of at least one class.

  • The starting point of any Java program is the method public static void main(String[] args).

  • Messages can be displayed in the console with these instructions:

    • System.out.println, Which displays a message with a line break at the end;

    • System.out.print, Which displays a message without a line break.

  • #

Related posts

Leave a Comment