Vous êtes sur la page 1sur 6

1.

Installing Java
We will install Java on your machine. The installation is actually done in two p
arts: the first is the JDK installation that is the development kit, or the comp
iler, the second step is the JRE that is a virtual machine to run applications d
eveloped in Java. You should already have the installation file downloaded from
Sun's site, then follow the steps to install the Java Virtual Machine: 1. Run th
e file to begin installation 2. At the license screen select "I accept the terms
in the license agreement" and click Next.
Figure 1.1 - Acceptance of License
3. In the Custom Setup window, we can choose some characteristics of the facilit
y, normally leave the default mode as shown below. Click Next.
Figure 1.2 - Installation Setup Development Kit
4. Well now just wait for the installation kit designed to install the virtual m
achine, which will run automatically after installing jdk.
Figure 1.3 - Installing Development Kit
5. In the window installation configuration of the virtual machine, we can modif
y some features of the facility, but since installing the development kit, usual
ly left in default mode as shown below. Click Next.
Figure 1.4 - Installation Setup Virtual Machine
6. The installation of the virtual machine allows you to install an integration
with browsers like Internet Explorer and Mozilla (Firefox). This integration ser
ves to we run Java programs from html pages. To do this simply select what you w
ant integration and click Next.
Figure 1.5 - Integration with Browsers
7. Okay, now just wait to install the virtual machine.
Figure 1.6 - Installing the Virtual Machine
8. There, the installation was completed successfully. Click Finish.
Figure 1.7 - Completion of Installation
2. Installing Eclipse
Installing Eclipse is very simple, just unzip the file to a desired location.
3. Initial Settings
We will have the first contact with the programming tool. I will show here the m
ain screens and settings to a better development environment. The first thing we
have to do is run the eclipse.exe that is inside the folder where the eclipse w
as unpacked. When running eclipse the first window that appears is a selection o
f Workspace as shown below.
Figure 3.8 - Select the Workspace
The Workspace is nothing more than a directory where the new projects will be cr
eated. There is a check box marked "Use this as the default and not ask again",
enable this option if this will cause the eclipse workspace use this as the stan
dard for future openings and no more shows this window. This is used when we hav
e only one workspace, but can use multiple workspaces to better organize our wor
k. Choose the Workspace with the Browse button and click OK. The first time we r
an the eclipse will see a screen Wellcome, this screen you can see some features
of Eclipse and tutorials, so we go ahead click Workbench as shown in the window
below or close this tab.
Figure 3.9 - Window Wellcome
Now with the Eclipse open we can begin to explore the windows and settings.
Figure 3.10 - Development Environment
Eclipse is a general-purpose tool that can be configured for various tasks using
plug-in. The Eclipse comes prepared to Java development, but we can easily inst
all a plug-in called CDT for programming in C / C + + using the same interface.
To organize these various programming interfaces, using the Eclipse perspectives
. We can see that we have Java enabled to view the upper right corner of the scr
een, as shown below.
Figure 3.11 - Perspective buttons
We can open other perspectives accessing the menu Window - Open Perspective. Alw
ays try to determine if you are in the Java perspective. In a perspective we can
configure it the way we see fit, for example, we can open other windows by acce
ssing the menu Window - Show View. Open the Console window, this window will be
very useful when we begin to plan. The screen can be as shown below.
Figure 3.12 - A Windows Console
On the left we have the Package Explorer window, where we can manage our separat
e classes for projects and packages. This window can be opened by accessing the
menu Window - Show View - Package Explorer. On the right we have the Outline win
dow, where we can see the structure of our classes, variables and functions. Thi
s window can be opened by accessing the menu Window - Show View - Outline. Below
is the Console window where we interact with our program,€seeing the printed v
alues and entering the input values. This window can be opened by accessing the
menu Window - Show View - Console.
4. How to create a class
The first thing we do is create a project. The project is a directory where will
be stored all our source code files and settings. The handling of projects and
classes is made from the Package Explorer window. Click your right mouse button
on the white area of the Package Explorer and select New - Project, as shown bel
ow.
Figure 4.13 - Menu Creation Project
In the New Project window, select the type of project you want to create. In our
case we create a Java Project or the Project Java. Click Next.
Figure 4.14 - Window Project Type
Clicking Next will go to the New Java Project window where we can change some se
ttings on our project. The first thing we can do is put the project name in Proj
ect Name field, in my case I chose the name "First Project". Another important s
etting is in Project Layout, where you can configure Eclipse to separate the sou
rces of binary files in two directories. This setting will cause Eclipse to crea
te two directories within your project: the src directory, where they will be th
e source files (. Java) and the bin directory where the files are compiled (. Cl
ass). With that we can improve the organization of our project. These settings a
re shown in the figure below. Click Finish to complete the project creation.
Figure 4.15 - Project Settings
Created the project, we can navigate your files through the Package Explorer win
dow, notice that Eclipse created the src directory where we will create our clas
ses.
Figure 4.16 - Project in Package Explorer
Now we can create our first class, so that just click with right mouse button ov
er the src directory and select New - Class.
Figure 4.17 - Menu Creation Class
In the New Java Class window, we can change some settings in our class. Initiall
y we will set the package name (1), the class name (2) and ask for Eclipse to cr
eate our class already with a main method (3). Click Finish.
Figure 4.18 - Window creation of new class
In Java classes are divided into packets for better organization and enables us
to create classes with same name in different packages. We'll understand it bett
er when we're seeing how to use classes, but it is good to start creating the ha
bit of using packages. The packages should follow a pattern of creation with all
lowercase letters. If you look at the directory tree in our project, we see tha
t each word separated by a period in the name of the package is a directory crea
ted in our project. The figure below shows the directory tree design.
Figure 4.19 - Tree Directory Project
The name of the Java class must also follow a pattern. The class name must have
a capital letter in each word, for example: HelloWorld, ControlarCadastro, etc.
OperaçãoSoma. The main method is our main method and it is through the impleme
ntation of our program starts. A class that has the main method can be called th
e class executable. After creating the class, our code appears in the center of
Eclipse and you can see on the right project information in the window Package E
xplorere left and the information of the class selected in the Outline window, a
s shown below.
Figure 4.20 - Development Environment
5. Running a program
Run a program in Eclipse is very easy, are just two clicks. First create a proje
ct called "First Project" with a class
executable called "Hello", then enter the code below into the scope of the metho
d main. Do not forget to put a package when creating the class.
System.out.println ("My first Java program");
The complete code should look something like this:
edu.ead.helloworld package; public class Hello (/ ** * @ param args * / public s
tatic void main (String [] args) (System.out.println ("My first Java program");)
)
To run our class first you must click with the right mouse button on the class n
ame in the Package Explorer and select Run As - Java Application. The next few p
lays can be done by pressing Ctrl + F11. When you run this application, we will
see the message in the Console "My first Java program, with this we can deduce t
hat the command System.out.println () is used to print information on the island
. Ln this case is to skip a line after writing the text in quotation marks. A qu
oted string in Java is called a String.
Figure 5.21 - Development Environment after the program
6. Running step by step
To train the execution step by step€need a program a little bigger. Create a ne
w project with a class called SomaSimples executable. The class code is shown be
low.
edu.ead.helloworld package; public class SomaSimples (/ ** * @ param args * / pu
blic static void main (String [] args) (/ / Variable declarations int primeiroVa
lor = 0; segundoValor int = 0, / / Assignment values primeiroValor = 5; segundoV
alor = 3 / / Implementation of complex calculation: D + int result = primeiroVal
or segundoValor / / output data System.out.println ("The sum is:" + result);
)
)
In the above two variables is declared integer, and primeiroValor segundoValor,
initially with zero. After the allocation of values, you create a variable calle
d result which receives the sum of primeiroValor with segundoValor and this resu
lt is displayed using the command System.out.println (). Some considerations: ï §
variable name should always start with a lowercase letter and the other words th
at form the name must start with a capital letter. Examples: primeiroValor, clie
ntname, telephone etc.. Where should we start variables with a value in its decl
aration, even if it is zero. Variables in Java can be declared anywhere in the c
ode, but can only be used in line after its declaration. Please note that, to sh
ow the result we used the command System.out.println () and println function par
ameter was made a sum between a String and an integer value. The Java language l
ets you concatenate String with another value, then the value is automatically c
onverted to String and added at the end of String.
ï § ï §
ï §
When running the generated code we will see the following output on the console:
The sum is: 8
Before we run step by step, we need to set a breakpoint called Breakpoint. Simpl
y take two clicks on the gray area on the left side of the line where you want t
he execution to stop that from this line, the program is executed step by step.
By doing this, you will see a blue dot in front of the line where the breakpoint
was inserted.
Figure 6.22 - Code with breakpoint
To start the execution step by step instead of selecting Run As - Java Applicati
on by clicking the right mouse button in the class name in the Package Explorer,
select Debug As - Java Application. As I had commented before, Eclipse works wi
th prospects for each task that the user wants to perform. In the case of an exe
cution step by step, the Eclipse is configured to automatically switch to the De
bug perspective, but he asks if that is what the user wants and should always do
this. In the Confirm Perspective Switch dialog, enable the "Remember my decisio
n" and then click Yes as shown below.
Figure 6.23 - Window switching confirmation perspective
By switching to the Debug perspective, the program will run and paused on the li
ne to place the breakpoint.
Figure 6.24 - Start of execution step by step
As shown in previous figure, we have a slightly more detailed view debug. Our co
de is being shown in the central region with the line of the breakpoint is indic
ated by a green background and a blue arrow on the gray area on the left side of
the line, next to the blue breakpoint dot. The Variables window is one of the m
ost important one is used to debug and verify or modify the value of each variab
le being used in this part of the code. Note that the variables initially primei
roValor segundoValor are zero and therefore has not yet run the line which conta
ins the breakpoint, which is assigned the value 5 to variable primeiroValor. Ano
ther window is important is the Expressions window that initially there is shown
in perspective, but click the Window menu and select Show View - Expressions. R
emember that to show any window on a prospect just access the menu Window - Show
View. The Expressions window is used to show any expression used in the code, f
or example, we can put the words "primeiroValor + segundoValor. For that just cl
ick with right mouse button in the Expression window, select Add Watch Expressio
n and typing the words you want in the window. Another way is to select the word
s in the code, clicking the right mouse button and select Watch.
Figure 6.25 - Creation of an expression from the Expressions window
Figure 6.26 - Creation of an expression from the code
The Expression window should show the expression and its value as shown below.
Figure 6.27 - Window Expression with the expression and its value
As we execute our program step by step, we check the values in the windows Varia
bles and Expressions. To run our program step by step, we use the basic command
that is debug the Step Over.€This command will execute one line of our program
at a time. The Step Over command can be executed by pressing F6 or by selecting
Run Press F6 key twice to execute two lines of our program and check the values
in the Variables and Expressions.
Figure 6.28 - Variables Window after executing
Figure 6.29 - Window Expressions after execution
Figure 6:30 - code after execution
We see that in the Variables window primeiroValor variable has the value 5 and t
he variable value is segundoValor 3. Expression in the window, we see that the v
alue of the sum of the two variables is 8 and is this value that is stored in th
e variable result if you execute another step in executing our code. But before
that we change the variable value segundoValor to 5. In the Variables window, cl
ick the variable value segundoValor, which should be 3 to 5 and change. Note tha
t the value of expression in the Expression window has been recalculated automat
ically.
Figure 6:31 - Changing the value in the Variables window
Figure 6.32 - Window Expressions with the recalculated value
Perform one step of your program by pressing F6 and check the variables in the V
ariables window. Finalize the implementation of its program by pressing F8 which
will cause the execution to continue normally until you find another breakpoint
or the end of the program. You can stop the execution of your program in half s
o select the menu Run - Terminate. To return to the Java perspective by clicking
on the Java perspective button on the top right of the eclipse.
Figure 6:33 - Java perspective Button

Vous aimerez peut-être aussi