You are on page 1of 26

MODULE OF INSTRUCTION

Getting to know the programming


environment

In this module we are going to know the programming environment of


Java. We are also going to discuss on how to write, compile and run
Java programs using Netbeans IDE (Integrated Development
Environment) and Text Editor.

At the end of this lesson you should be able to:

- Write simple Java code in Text Editor and Netbeans.

- Compile and run Java code using Command Line and


Netbeans.

- Differentiate the three types of error, the syntax errors, runtime


errors and logical errors.

A. The “Hello World!” application, your first Java Program

In this section we are going to write, compile and run a Java program
in Text Editor and Command Line. We are going to create a simple
program that will display the words “Hello World!”. Below is the
source code of our first program:
public class HelloWorld{
public static void main(String args[]){
System.out.println("Hello World!");
}
}

Before we can run any Java program, we will need the following:

- Java Standard Edition (SE) Development Kit 8 (JDK8).

- A text editor, an example of this is Notepad.

Let’s start creating the “Hello World” program. The following are the
steps to create the program:

Step1 : Start the text editor.

Computer Programming 2 1
Week 2 Getting to know the programming environment

In this case we are going to use Notepad. Notepad is found in the


Accessories folder of the Start Menu.

Step 2: Write the source code in Notepad text editor.

2
MODULE OF INSTRUCTION

3. Save the source file.

We need to save the file as HelloWorld.java but before we do that we


need to first create a separate folder (let's name it JAVAPROGRAMS)
in drive C, the folder will be also used for all our future Java source
codes.

To save the file in notepad, on the menu bar, click file then select
"save as".

Computer Programming 2 3
Week 2 Getting to know the programming environment

The Save dialog box will show up, navigate to drive C and then select
the folder that you created a while ago.

On the file name input box, enter "HelloWorld.java" and change the
"Save as type" to "All Files" and then click save.

4
MODULE OF INSTRUCTION

Step 4. Compiling the source code using the windows command


line.

The first thing that we need to do to compile the source code is launch
the command prompt, click the start menu and on search bar type
command or cmd.

When the command prompt open up, by default it will take you to the
current user directory. In the example below the default directory is in
Administrator.

Computer Programming 2 5
Week 2 Getting to know the programming environment

We need to navigate to the folder where the Java source code was
saved or we need to go to “C:\JAVAPROGRAMS”. On the command
line type “cd C:\JAVAPROGRAMS” then press enter. You are now
inside the JAVAPROGRAMS directory. The “cd” command stands
for change directory; it is used to change the current directory.

Let us check whether we are in the right directory and see if the Java
source file was saved in this directory. On the command line type “dir”
then press enter, your Java source file should be displayed. “dir”
command is used to display files and subdirectories inside a directory.

6
MODULE OF INSTRUCTION

To compile the Java source file we need to enter “JavaC [filename]”


in the command line, in our case we should enter “JavaC
HelloWorld.java”.

As we discuss in the phases of a Java program, the compilation


process generates bytecodes, in this case the HelloWorld.class will be
created and this contains the actual Java bytecode.

Computer Programming 2 7
Week 2 Getting to know the programming environment

To run the Java program, we need to enter “Java [filename]” or in our


case “Java HelloWorld”. You have just run your first program and
displayed the message, “Hello World!”

Resolving an error: "javac is not recognized as an internal or


external command"

If there is a problem occuring during the compilation like the figure


below, we need to set the JavaC path in the command prompt because
Java compiler or JavaC does not recognize in command prompt. What
we perform during the compilation is call or executes JavaC program
using command line, the error occurs because we are calling a program
which is not present in the current directory. To set the path, we need
to locate the directory of the JavaC which is by default it is located in

8
MODULE OF INSTRUCTION

"C:\Program Files\Java\jdk[version]\bin". Once the path was set for


JDK all files and programs inside the path directory (such as JavaC,
Java, etc.) will be available to be executed in the command line.

There are two ways to set Java path, one is temporary and other one is
permanent.

1. Setting the temporary path of JavaC


a. Launch command prompt
b. Enter the path command, for example,
“path C:\Program Files\Java\jdk1.7.0_03\bin”

Computer Programming 2 9
Week 2 Getting to know the programming environment

2. Setting permanent path of JDK.


a. On your desktop, right click the computer icon, then
click properties.

b. Click the advance system settings

c. Select the Advance tab and click the environment


variables

10
MODULE OF INSTRUCTION

d. Click the new button of user variable.

Computer Programming 2 11
Week 2 Getting to know the programming environment

e. On the variable name field enter the “path” word

12
MODULE OF INSTRUCTION

f. Copy the path of the JDK bin folder.

Computer Programming 2 13
Week 2 Getting to know the programming environment

g. Paste the copied path in the variable value field. Then


click all the OK buttons from all the windows you have
opened.

B. Errors

The example source code that was provided in this module is


free from error, if it happens that you still encountered error
during compilation aside from unrecognized JavaC you
probably got a syntax error and this section will be able to help
understanding your error. There are three types of error that
you might encounter in Java programming:

14
MODULE OF INSTRUCTION

▪ Syntax errors
▪ Runtime errors
▪ Logical errors

Syntax Errors
The errors occur when the syntax of the language is
violated, specifically when a word or symbol was not correctly
placed in an instruction or statement of the program.

▪ Misspelled keyword, variable and method names or


incorrect used of capitalization.

The figures above demonstrate a program that has


syntax errors. After compilation of the program, the
compiler encountered two errors. The first error
message tells us that we had an error on line 1 of our

Computer Programming 2 15
Week 2 Getting to know the programming environment

program and it is pointed on the word publix, which is a


misspelled word for public. The second error message
suggests that we had an error in line 6 and pointed in
between of the words Public and static, as you can see
the first letter of the word Public is in upper case and it
is expected to be in lower case.

▪ Missing semi-colon at the end of the statement or


incorrect used of symbols.

The program above obviously produces errors, the


violations are, first the use of comma (,) instead of dot
(.) and the second one is missing semi-colon at the end
of the statement. You can compile the program above to
see the generated error message.

▪ Brackets such as curly braces “{}”, parentheses “()“


and square brackets “[]” is not properly matches. Never
forget to enclose your brackets, make it a habit to type
the brackets in pair (opening and closing bracket).

Runtime Errors

Runtime errors only occur after compilation (when there is


no syntax error in the program) and running your program.
Runtime is when the program is running therefore you can
only encounter runtime error during execution of the
program or when you are using your program. The
common examples of these are, trying to open a file, but

16
MODULE OF INSTRUCTION

the file doesn’t exist or is corrupted and when you try to


execute division by zero.

The figure above is a sample program that has no syntax


error, but will produce runtime error and it has a statement
that performs a division by zero. The figure below shows
that the program was successfully compiled and didn’t
produce syntax error, however when we tried to run the
program, we encountered runtime error. The message tells
us that we are executing division by zero, which is not
possible or incorrect.

Logical Errors

Computer Programming 2 17
Week 2 Getting to know the programming environment

Logical errors also occur once the program is in use and


these errors will not interrupt the execution of the program.
Errors are those where the program is running smoothly,
but produces unwanted or unexpected result from what you
designed. The common examples of these are:

▪ Incorrect used of mathematical operation like using


of + symbol for subtraction.
▪ Displaying the incorrect message.
▪ Using data from incorrect source.

The figure below is an example that demonstrates a logical


error. As you can see there is no error message being
shown or the program was not interrupted, however we can
notice that there is something wrong in the output, we want
the program to show the addition of “2” and “2” but instead
it displayed the concatenated number “22”. Logical errors
are considered the most difficult type of errors to fix
because it is not clear where the errors originate since it
didn’t display any information about the error.

18
MODULE OF INSTRUCTION

Using NetBeans
In our previous discussion, we tackled the hard way of
running Java program. Let’s now try the easiest way of
writing and running the Java program.

In this part of the lesson we are going to discuss writing


and compiling Java program using NetBeans. Netbeans is
an Integrated Development Environment or IDE, it is a
programming environment that provides comprehensive
facilities to programmers for Developing Java programs.
NetBeans contains a source code editor, GUI Builder,
Compiler, interpreter and a debugger.

Step 1: Start NetBeans

To open the NetBeans program, double click the NetBeans


shortcut icon in your desktop.

The figure below shows the graphical user interface (GUI)


of the NetBeans IDE.

Computer Programming 2 19
Week 2 Getting to know the programming environment

Step 2. Creating a project.

To create a project, in the IDE menu bar choose File then


click New Project.

20
MODULE OF INSTRUCTION

After clicking the New Project, a New Project dialog bar


will show up. On the category list, select Java and on the
Project list, select the Java Application, then click the Next
button.

Computer Programming 2 21
Week 2 Getting to know the programming environment

In the New Application Dialog, do the following (as shown


in the figure below):

▪ In the Project Name field change the value to


JavaPrograms.
▪ Leave the default value of the Project Location, by
default our project is located in
C:\Users\<user>\Documents\NetBeansProjects
directory and all the files will be saved in
C:\Users\<user>\Documents\NetBeansProjects\Jav
aPrograms.
▪ Leave the Use Dedicated Folder for Sharing
Libraries unchecked.
▪ On the Create Main Class field, enter
“HelloWorld” and then click the Finish button.

The Java Application project is now created and opened in


the IDE. The IDE will have the following components:

▪ Projects window displays all projects loaded in the


IDE and it is presented in a tree view. This window
shows the components of the project such as source
files, libraries, etc.

22
MODULE OF INSTRUCTION

▪ Source Code Editor Window where you can write


Java source code.
▪ Navigator window where you can navigate
elements within the selected class.

Step 3. Writing your program in NetBeans IDE...

Since you have left the Create Main Class checked in the
New Application dialog the NetBeans IDE have generated
HelloWorld.java file that contains code that is shown in the
source code editor window.

Let us now create a program that displays “HelloWorld”


message. Replace the line of code:

// TODO code application logic here

with this statement:

System.out.println(“Hello World!”);

Computer Programming 2 23
Week 2 Getting to know the programming environment

Save the code by selecting File on the menu bar and then click
save or we can simply press CTRL + S.

Step 4. Compiling and Running your program in NetBeans


IDE.

NetBeans IDE has Compile on Save feature, it


automatically compiles the Java source file after saving.
Therefore, we don’t need to manually compile the project
in order to run it.

NetBeans IDE has a real time syntax error checker, the


error icon (red glyph in the left margin) and red underline
on the statement will automatically show up if you typed a
misspelled keyword or forgot to enter the required symbol
(anything that violates the syntax).

24
MODULE OF INSTRUCTION

There are three ways to run your program in NetBeans


IDE:

▪ Select the Run on the menu bar then click Run


Project.

▪ Click the Run Icon on the Tool bar

▪ Press F6

Computer Programming 2 25
Week 2 Getting to know the programming environment

The output of the program will be shown in the output


window of NetBeans IDE.

LESSON SUMMARY:

1. You can write java program using notepad and compile it using
windows command line.

2. Set Java compiler path in command line to resolve "javac is not


recognized as an internal or external command" error.

3. There are three types of error in java these are Syntax errors, run
time errors and logical errors.

4. NetBeans IDE is a programming environment that provides


comprehensive facilities to programmers for Developing Java
programs.

26

You might also like